3

I have a requirement where I send multiple emails to client.

But I have a issue while sending email to multiple email Ids.

For example, if I write

s@t.com;r@t.com

It would throw me an error stating email id is not correct.

I want the email ids to be separated by semicolon but it seems my regex is not supporting semicolons.

Proper format should be :

s@t.com;d@r.com

Should not allow

s;r@t.com

Regex used

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

Please guide.

Bilesh Ganguly
  • 3,000
  • 2
  • 31
  • 50
sTg
  • 3,848
  • 12
  • 60
  • 105
  • 2
    Why not simple `string.split(";")` – Jens Jul 21 '16 at 08:11
  • How do you send the emails? You should probably split the emails on the `:` and set multiple recipients. – Krzysztof Krasoń Jul 21 '16 at 08:11
  • Actually we have set in our JPA class for the field using @Pattern – sTg Jul 21 '16 at 08:12
  • so i need to have a format to include ; after an email id. No other options. – sTg Jul 21 '16 at 08:14
  • possible duplicate of http://stackoverflow.com/questions/9809357/regex-for-validating-multiple-e-mail-addresses – yash Jul 21 '16 at 08:17
  • 1
    You have a regex for 1 email, right? You want to check if the string contains email + ( `;` + email ) {any number of times}, right? Then, use the `myregex = "['_A-Za-z0-9-+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})"; String final_pattern = regex + "(?:;" + regex + ")*";`. Note the `^` and `$` are removed from the `regex` pattern. – Wiktor Stribiżew Jul 21 '16 at 08:39
  • possible duplicate of http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address – krzydyn Jul 21 '16 at 08:40
  • 1
    Guys, this is not a dupe question for validating emails. OP has a regex for that, and only needs to make sure multiple semicolon separated emails get through. – Wiktor Stribiżew Jul 21 '16 at 08:42

2 Answers2

2

You have a regex for 1 email.

Now, you want to check if the string contains email + ( ; + email ) {any number of times}.

You need to use your previous pattern as a block without anchors and build the final pattern like this:

String myregex = "['_A-Za-z0-9-+]+(?:\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(?:\\.[A-Za-z0-9]+)*\\.[A-Za-z]{2,}"; 
String final_pattern = "\\A" + regex + "(?:;" + regex + ")*" + "\\z";

See the regex demo

Note that \A is the unambiguous start of string and \z - the very end of string anchors.

Also note that the + inside a character class loses its special meaning of a quantifier (1 or more times), and becomes/is treated as a mere literal + symbol. I also removed unnecessary groupings and turned all capturing groups into non-capturing for a "cleaner" matching (if you need no captured values, why store them in a stack for each group?).

Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
0

I think that if you use string.split("_"); you will be fine. I used for mine bodyremoveregex=(ÎÎÎÎΣ On Line)([,]* [0-9]:[0-9], [0-9]/[0-9])*__(/**/) and it worked correctly.You just have to add _ between the expressions.For some reason it doesn't show the _ symbol in my answer so imagine that everywhere I have __ I mean _.