validating only .com.br regex

1

Personnel was modifying a regex to validate only specific domains that end with yahoo.com.br, terra.com.br, bol.com.br, hotmail.com.br. So gmail.com, or Provider.net.br would be invalid.

Then I made the regex below:

const std::regex pattern("([a-zA-Z0-9._]+@[hotmail|terra|yahoo|bol]+(?:[.][com]{2,4})?(?:[.][br]{1,2})?)");

But you are also validating mail that ends only with .com or if I type: [email protected] it validates and could not validate.

I have tried to do this as follows:

const std::regex pattern("([a-zA-Z0-9._]+@(?:[hotmail.com.br|terra.com.br|yahoo.com.br|bol.com.br]{2,4})?)");

But then it returns everything invalid. Any suggestions?

    
asked by anonymous 17.12.2017 / 05:27

1 answer

2

It's not working because, first of all, you're putting a ? in br. This in regex means that the value is optional.

The second way, when you tell {2,4} , you mean for the software to only get the data between 2 and 4 characters, so it only takes half the value and consequently valid .

Another problem is that you are using with , br and providers within the brackets. This causes the code to pick up information that has the same letters, not necessarily the word. Ex: It validates values such as hootmail , hhhooottmmmaaiill , etc. The ideal here is to use relatives.

For your case, the regex below should work. ([a-zA-Z0-9._]+@(?:hotmail|terra|yahoo|bol)\.(?:com\.br))

Regex

Regex Debugger

Regex Tests

    
17.12.2017 / 06:02