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
[email protected];[email protected]
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 :
[email protected];[email protected]
Should not allow
s;[email protected]
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.
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?).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With