0

I'm using the following regular expression to validate email addresses:

    ([_A-Za-z0-9-+]+(.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(.[A-Za-z0-9]+)*(.[A-Za-z]{2,})$)

but I am getting Catastrophic Backtracking in Regular Expression when the string is:

   abc@damkovnai.un;efg@damkovnai.un;hij.kumar@damkovnai.un;klm.verma@damkovnai.un;

When I remove the last semicolon, the result is true, but when the semicolon exist in last position in the string then the output is Catastrophic Backtracking, even though I want the result to be false.

MANOJ
  • 41
  • 7

1 Answers1

3

Fixing the catastrophic backtracking

The problem is that you forgot to escape the dots, asking for any character instead of an actual dot. That causes the string to be parsable in a very large number of ways, which is likely the cause of the catastrophic backtracking.

So you want:

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

Match a list of e-mail addresses

If you want to use this to match a semicolon-separated list of e-mail addresses without spaces, you can use this:

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

Details:

  • start the pattern with ^ to match from the beginning,
  • add ([,;]|$) at the end to say each address has to end with ; or , or be at the end of the string,
  • use * to repeat the whole group, and
  • finish the pattern with another $ to make sure the whole string is matched to the end.

Demo

joanis
  • 10,635
  • 14
  • 30
  • 40
  • above regular expression is not validating full string. – MANOJ Sep 01 '19 at 07:29
  • @MANOJ You didn't define exactly how you wanted the whole string validated in the question, you just asked how to fix the catastrophic backtracking. If you specify how you want the whole string validated, I could adjust my answer: should the string be a list of e-mail addresses with semicolons in between? Are spaces allowed between addresses? Is any prefix tolerated or does it have to start with an e-mail address? – joanis Sep 01 '19 at 12:37
  • 1. List of e-mail addresses will be semicolons or comma in between 2. spaces not allowed between addresses 3. it have to start with an e-mail address. – MANOJ Sep 02 '19 at 11:26
  • @MANOJ - there, I just added a whole list solution. – joanis Sep 02 '19 at 13:06
  • @MANOJ does this modified solution work for you? – joanis Sep 04 '19 at 18:46