-2

Email format is like : local_part @ domain_part.com

Local_part & domain_part should not begin or end with special characters ( @#&()*,./{}<>^%[]~`!$=\|;:? ) including hyphen (-)

Local_part & domain_part may contain above special characters like (#$%&*- etc) in middle only.

Limit of lengths for local_part is 64 & domain_part is 255 characters respectively.

domain_part must not contain all numbers

Sai
  • 11
  • 4

2 Answers2

1

Updated 7/27/2018

^(?![.!#$%&'*+/=?^_`{|}~-])(?:[a-zA-Z0-9]|(?:(?:([.])(?!\1)|[!#$%&'*+/=?^_`{|}~-]))(?!@)){1,64}@(?=.{1,255}$)(?!\d+$)[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])?)*$

https://regex101.com/r/dxCmEn/1

 ^                             # BOS
 (?! [.!#$%&'*+/=?^_`{|}~-] )  # Local does not start with one of these

 (?:                           # Local
      [a-zA-Z0-9] 

   |  
      # Special chars
      # Local does not end with one of these

      (?:
           # Option 1
           (?:
                ( [.] )                       # (1), Not a consecutive special char (specific), add more
                (?! \1 )                      # 
             |                              # or,
                [!#$%&'*+/=?^_`{|}~-]         # One of these other special chars, remove from here
           )

           # Option 2
           # |  ( [.!#$%&'*+/=?^_`{|}~-] )     # (1) Not a consecutive same special char 
           #    (?! \1 )                        

           # Option 3
           # |  [.!#$%&'*+/=?^_`{|}~-]         # Not a consecutive any special char
           #    (?! [.!#$%&'*+/=?^_`{|}~-] )  

           # Option 4, Original
           #  |  [.!#$%&'*+/=?^_`{|}~-]        # Any special char is OK
           #     

      )
      (?! @ )

 ){1,64}                       # 1 to 64 local characters

 @ 
 (?= .{1,255} $ )              # 1 to 255 domain characters
 (?! \d+ $ )                   # Domain must not contain all numbers

 [a-zA-Z0-9]                   # Domain
 (?:
      [a-zA-Z0-9-]{0,61} 
      [a-zA-Z0-9] 
 )?
 (?:
      \. 
      [a-zA-Z0-9] 
      (?:
           [a-zA-Z0-9-]{0,61} 
           [a-zA-Z0-9] 
      )?
 )*
 $                             # EOS          
  • Did not work above expression....Please find exact requirement updated again. – Sai Jul 16 '18 at 14:42
  • `domain_part may contain above special characters like (#$%&*- etc) in middle only.` These characters I think are illegal in the domain part. –  Jul 16 '18 at 16:09
  • @Sai - Posted an updated regex. I did not account for those _special_ characters in the domain, sorry. Everything else works with your updated question. –  Jul 16 '18 at 16:25
  • Thanks sln for the info, it worked after few updates on top of it. – Sai Jul 24 '18 at 12:19
  • @Sai - If it solved most of your problem, you should accept the answer. –  Jul 24 '18 at 18:25
  • Further enhancement requirement came of not allowing special characters consequently in above regex . Eg : test....test@gmail.com is not acceptable, currently its accepting with provided regex. Could you let me know where it needs update. – Sai Jul 26 '18 at 08:12
  • @Sai - `test` this->....<-here `test@gmail.com` is in the middle and it should pass no ? –  Jul 26 '18 at 19:17
  • I mean continuously more then 2 dots are not allowed. – Sai Jul 27 '18 at 07:12
  • @Sai - Ok, added 4 options to choose from. Select one of them (uncomment), Un-select the others (comment). –  Jul 27 '18 at 18:15
0

So based on your limited description it seems like you're looking for something like this?

Here is the pattern: ^(?<localpart>[^-].*[^-])@(?<domainpart>[^-].*[^-])\..{2,3}$

It saves them into named capture groups, and it assumes that both the localpart and domainpart contain at least 2 characters (which they should). Neither parts are allowed to begin or end with -.

Are there any other requirements or does this pretty much cover it?

emsimpson92
  • 1,779
  • 1
  • 9
  • 24
  • Did not work above expression....Please find exact requirement updated again. – Sai Jul 16 '18 at 14:40
  • Thanks sln for the info, it worked after few updates on top of it. – Sai Jul 24 '18 at 12:19
  • Further enhancement requirement came of not allowing special characters consequently in above regex . Eg : test....test@gmail.com is not acceptable, currently its accepting with provided regex. Could you let me know where it needs update. – Sai Jul 26 '18 at 08:16