Regular expression for e-mail in C ++

3

I have the following regular expression in a format validation function in C ++ 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

    
asked by anonymous 08.02.2017 / 03:20

3 answers

1

After much searching on the net, I ended up finding a regex that would serve both cases in 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.

    
03.07.2017 / 03:48
2

First of all, I do not know what the purpose of b is, I think it could be the \b that delimits the boundaries of string .

Subtracting from REGEX is:

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

The matchs you entered does not close either.

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

Assuming then that REGEX is:

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

From this, the problem you have is in the @[A-Z0-9.-]+ part, because after @ note that it is also searching for . which generates its error, since it considers as part of capture the .com itself %.

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

To adjust this problem you will need to change% from% to% with%. REGEX

    
08.02.2017 / 16:37
1

The problem you are having is that @[A-Z0-9.-]+ is considering a point, we do not want this behavior.

I did not understand very well the b s played by the regex of the question, but doing some tests the regex that would fit better for this case would be:

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

Note that ^ at start and $ indicate that we want strings exactly thus, without partial matches.

Test case

    
08.02.2017 / 03:49