2

I am trying to achive regex expression: I need regex with email, I am using that:

\S+@\S+\.\S+

And I want to repreat it X times with ; separator. I cant figure this out... For example pattern should allow following strings:

[email protected]
[email protected];[email protected];[email protected];[email protected]

But should not allow for example following string:

[email protected];example2email.com;[email protected];
zxf18682
  • 21
  • 2

1 Answers1

1

In general, the X-separated list of entities is matched with ^y(?:Xy)*$ like pattern.

Here, there is a caveat related to what you match with y and X: \S matches ; chars, so the semi-colon has to be "removed" from the \S pattern, hence the \S should be replaced with [^\s;] in the resulting pattern.

You can use

^[^\s;]+@[^\s;]+\.[^\s;]+(?:;[^\s;]+@[^\s;]+\.[^\s;]+)*$

See the regex demo.

Details:

  • ^ - start of string
  • [^\s;]+@[^\s;]+\.[^\s;]+ - one or more chars other than whitespace and ;, @, one or more chars other than whitespace and ;, . and then again one or more chars other than whitespace and ;
  • (?: - start of a non-capturing group:
    • ; - a semi-colon
    • [^\s;]+@[^\s;]+\.[^\s;]+ - email-like pattern (described above)
  • )* - end of the non-capturing group, repeat zero or more occurrences
  • $ - end of string.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563