Regular expression for some email rules

I used a regular expression for email formats, which I thought was fine, but the client complains that the expression is too strict. Therefore, they returned with the following requirement:

The email must contain the character "@" and end with either .xx or .xxx, i.e. (. nl or .com). They are happy with this to pass the test. I started the expression to see if the string contains the "@" symbol below

^ (? =. * [@])

it seems to work, but how to add the last requirement (should end in .xx or .xxx)?

+3
source share
6 answers

A regex just fulfilling your two requirements:

^.+@.+\.[a-zA-Z]{2,3}$

, , .

+2

          ^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
            @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
            @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$

http://www.ultrapico.com/Expresso.htm!

+2

RegEx, . , - , :

[^@]+@.+\.\w{2,3}$

:

  • [^ @] +: , @
  • @: @
  • . +:
  • \: a.
  • \w {2,3}: 2 3 - (a-zA-Z)
  • $:
+2

:

([\w-\.]+)@((?:[\w]+\.)+)([a-zA-Z]{2,4})\be(\w*)s\b

A good tool to test our regular expression: http://gskinner.com/RegExr/

+1
source

you can use

[@].+\.[a-z0-9]{2,3}$
0
source

This should work:

^[^@\r\n\s]+[^.@]@[^.@][^@\r\n\s]+\.(\w){2,}$

I tested it against these invalid emails:

@exampleexample@domaincom.com   
example@domaincom  
exampledomain.com  
exampledomain@.com  
exampledomain.@com  
example.domain@.@com  

e.x+a.1m.5e@em.a.i.l.c.o  

some-user@internal-email.company.c  
some-user@internal-ema@il.company.co  
some-user@@internal-email.company.co  

@test.com  
test@asdaf  
test@.com    
test.@com.co  

And these are valid letters:

example@domain.com  
e.x+a.1m.5e@em.a.i.l.c.om  
some-user@internal-email.company.co 

change

This seems to confirm all the addresses from this wikipedia page, although it probably also allows the use of some invalid emails. The brackets will be broken into everything before and after @:

^([^\r\n]+)@([^\r\n]+\.?\w{2,})$

niceandsimple@example.com
very.common@example.com
a.little.lengthy.but.fine@dept.example.com
disposable.style.email.with+symbol@example.com
other.email-with-dash@example.com
user@[IPv6:2001:db8:1ff::a0b:dbd0]
"much.more unusual"@example.com
"very.unusual.@.unusual.com"@example.com
"very.(),:;<>[]\".VERY.\"very@\\ \"very\".unusual"@strange.example.com
postbox@com
admin@mailserver1
 !#$%&'*+-/=?^_`{}|~@example.org
"()<>[]:,;@\\\"!#$%&'*+-/=?^_`{}| ~.a"@example.org
" "@example.org
üñîçøðé@example.com
üñîçøðé@üñîçøðé.com
0
source

Source: https://habr.com/ru/post/1733194/


All Articles