1

Regex Used:

EMAIL_VALID_REGEX = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"+"[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*(\\.[A-Za-z]{2,})$";

Now i need to allow apostrophe before @ and also it should not allow apostrophe as first and last character and also two consecutive apostrophe before @.

Can someone modify the above regex to handle the mentioned scenarios and can you explain it?

Valid emails: test@gmail.com, test's@gmail.com, t@gmail [Even if single character is passed before, it is valid] Invalid emails: 'test@gmail.com, test'@gmail.com, 'test'@gmail.com, test''s@gmail.com

Karthick
  • 13
  • 4
  • Replace `(\\.[_A-Za-z0-9-]+)*@` with ``([.'’][_A-Za-z0-9-]+)*@`` and it should suffice. – Wiktor Stribiżew Aug 31 '21 at 10:42
  • Thanks Wiktor Stribizew. it is working fine. Can you please explain me the regex posted and how your solution is working. As basics, i know , this ^ indicates start , [] character class, () indicates group class, + indicates one or more characters , * indicates zero or more characters. kindly help me to understand it better. Thanks in advance, – Karthick Aug 31 '21 at 10:57

1 Answers1

1

You can replace (\\.[_A-Za-z0-9-]+)*@ with ([.'’][_A-Za-z0-9-]+)*@:

^[_A-Za-z0-9+-]+(?:[.'’][_A-Za-z0-9-]+)*@[_A-Za-z0-9-]+(?:\.[_A-Za-z0-9-]+)*\.[A-Za-z]{2,}$

Define in Java with

EMAIL_VALID_REGEX = "^[_A-Za-z0-9+-]+(?:[.'’][_A-Za-z0-9-]+)*@[_A-Za-z0-9-]+(?:\\.[_A-Za-z0-9-]+)*\\.[A-Za-z]{2,}$"

See the regex demo

The [.'’] part is a character class matching ., ' or .

The ^[_A-Za-z0-9+-]+(?:[.'’][_A-Za-z0-9-]+)*@ part now matches

  • ^ - start of string
  • [_A-Za-z0-9+-]+ - one or more underscores, hyphens, plus, letters or digits
  • (?:[.'’][_A-Za-z0-9-]+)* - zero or more occurrences of
    • [.'’] - ., ' or
    • [_A-Za-z0-9+-]+ - one or more underscores, hyphens, plus, letters or digits
  • @ - a @ char.

If there is a comma, or apostrophe, they will be allowed only in between the chars matched with the [_A-Za-z0-9-] character class, as both occurrences of [_A-Za-z0-9-] patterns on both ends of [.'’] is quantified with + (i.e. they require at least one char to match).

To apply the same restriction to -, _ and +, use

EMAIL_VALID_REGEX = "^[A-Za-z0-9]+(?:[+_.'’-][A-Za-z0-9]+)*@[_A-Za-z0-9-]+(?:\\.[_A-Za-z0-9-]+)*\\.[A-Za-z]{2,}$"

See this regex demo.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Hi Wiktro, Thanks for the explanation. Actually we are already allowing _-.(underscore, hyphen, dot) in our existing regex. So for this example test_-.s@gmail.com has to be failed. but it is showing as success since we are allowing _ in character class... Can you please let me know, how to include that condition [ie, _-.] and ' , if it comes together, then it has to fail – Karthick Aug 31 '21 at 11:24
  • @Karthick Try [this variation](https://regex101.com/r/sVcXc0/2). – Wiktor Stribiżew Aug 31 '21 at 11:28
  • Wiktor, i tried with the regex shared in the link. Two scenarios are success, but it has to be failed . "ka_'r@gmail.com" , "ka-'r@gmail.com" . But this one is working fine. "ka.'r@gmail.com" . Since hyphen, underscore and dot are already allowed, we should not allow consecutive special characters like _' or -' – Karthick Aug 31 '21 at 11:33
  • @Karthick Those strings are [not matched](https://regex101.com/r/sVcXc0/3). – Wiktor Stribiżew Aug 31 '21 at 11:54
  • Thanks Wiktor, i forgot to remove the _ from character class. Now it is working fine. – Karthick Aug 31 '21 at 12:01