validating only .com.br regex

Asked

Viewed 118 times

1

Staff was modifying a regex to validate only specific domains ending with yahoo.com.br, terra.com.br, bol.com.br, Hotmail.com.br. Then gmail.com, or provider.net.br would be invalid.

So I did the regex below:

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

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

I’ve tried to do it this way:

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 all invalid. Any suggestions?

1 answer

2


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

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

Another problem is that you are using the with, br and the providers, inside 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 in this case 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

  • very good now based on this I will write another to validate domains .net.org.br

Browser other questions tagged

You are not signed in. Login or sign up in order to post.