Regular expression for email in C++

Asked

Viewed 610 times

3

I have the following regular expression in a C++ format validation function using regex, but it is validating non-standard formats that I want.

b[A-Z0-9._%-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}b\\.[A-Z]{2,4}b

    Email: [email protected] Valid
    Email: [email protected] Valid
    Email: [email protected] Valid (não pode validar)
    Email: [email protected] Valid
    Email: [email protected] Valid
    Email: [email protected] Valid
    Email: [email protected] Valid
    Email: [email protected] Valid
    Email: [email protected] Valid
    Email: [email protected] Valid
    Email: [email protected] Valid

the problem I want to emphasize is the regular expression would have to validate only formats like:

.com
.com.br
.net
.net.br
.org
.org.br

and formats such as:

 @terra.com.br
 @bol.com.br
 @yahoo.com.br
 @hotmail.com.br
 @hotmail.com

ie the expression should not validate anything after . br

3 answers

2

First of all I don’t know what the purpose of b, I believe it could be the \b defining the boundaries of string.

Vanishing that to REGEX be it :

/[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\.[A-Z]{2,4}/

The matchs that you reported also do not close.

  • I believe this part is duplicated \.[A-Z]{2,4}.
  • And I suppose you’re using the modifier i (case-insensitive).

Assuming then that to REGEX be it :

/[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/i

Starting from that the problem you have this on the part @[A-Z0-9.-]+, because after the @ note that he is also researching the . which generates his mistake, because he considers as part of the capture the own .com.

To resolve this, you can change to @[A-Z0-9-]+, however will validate only with a point .com and not .com.br. REGEX.

To adjust this problem you will need to change \.[A-Z]{2,4} for (\.[A-Z]{2,4}){1,2}. REGEX

  • Worst that still not validating correctly look code I’m trying to make this regex work already some three days ago http://pastebin.com/evVcm7FE I also used this one as this on the site REGEX const Std::regex Pattern("[A-Z0-9._%-]+@[A-Z0-9-]+(\.[A-Z]{2,4}){1,2}"); e mesmo assim ainda nao valida

1


After so much snooping on the net, I ended up finding a regex that fits both cases in both validation and emails:

^[a-z0-9.][a-z0-9._]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:[.][a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)?(?:[.][a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)?

.com 
.com.br
.net.org.br

And the formats

.com.br.net are invalid according to regex.

  • I reworked your answer, but I’m not sure if it was legal. I recommend reviewing it for this.

1

The problem you’re having is that @[A-Z0-9.-]+ is considering a point, we don’t want this behavior.

I didn’t quite understand them bs played by the question regex, but making some tests to regex which would best fit for this case would be:

^([A-Z0-9._%-]+@[A-Z0-9-]+\.[A-Z]{2,4}(\.[A-Z]{2,4})?)$

Note that ^ at the beginning and $ indicate that we want strings exactly so no partial handouts.

Test case

  • it continues to validate in the same way http://pastebin.com/L9rqeqEv

  • Yours is \.[A-Z]{2,4}?( where mine is \.[A-Z]{2,4}(, that’s not the problem?

  • not because it was already so . [A-Z]{2,4}(, and still validating formats like: .com.br.net

  • Got it, updated the answer, could you check again please? Can’t use your code here... You’re even giving Valid to any string. ?

Browser other questions tagged

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