-1

Hi guys i have a requirement such that i have to validate email addresses seperated by semicolon.For example "test@test.com; test@Tes.com" something like this.I am using the below regex :

(([^@]+@[^\s@]+\.[^\s@;]{2,}\;{0,1}\s*))+

The problem here it the regex works even if there is no semicolon in between but with space eg "test@test.com Test@tes.com".But That is not correct.so can anyone tell me how to achieve this?Also when there is a single email id no need of semi-colon in between

Rajan Mishra
  • 1,099
  • 2
  • 13
  • 26
  • No..Its not the same.Actually that post was for regular expression.Mine i have the regular expression.But the space inbetween instead of semicolon also matching.Need to avoid that and how to do it is my question – Deepak Ramakrishnan Kalidass Oct 09 '18 at 06:40
  • Another common way to achieving your requirement is to split the email string using ; as separator and then test for email validation. – Mrnell Oct 09 '18 at 06:58

2 Answers2

0

found a working regex. Tested till 3 emails.

([^@]+@[^\s@]+.[^\s@;]{2,})+(\;([^@]+@[^\s@]+.[^\s@;]{2,}){1,1})*

Tested samples :

  1. test@test.com;sample@google.com;dsdsd@dsd.sd -> true
  2. test@test.com;sample@google.com dsdsd@dsd.sd -> false
  3. test@test.com -> true
  4. test@test.com; -> false

Key thing here is to follow \w+(.\w+)+ pattern. Hope this helps

Ajanthan
  • 108
  • 2
  • 10
0

Try regex (?!.*;$)^([-\w]+?@\w+\.\w+(?:\s*;\s*)?)+$

This will match any no. of emails separated by a semicolon. The negative lookahead (?!.*;$) will not match email with a semicolon at last.

Regex

Nambi_0915
  • 961
  • 4
  • 21