0

I need to match a regex on Java when a string does NOT contains an email anywhere in the string: the beginning, middle or end. For example:

string with no email [match]

myMail@mail.com string with email at the start [no match]

String with myMail@mail.com in the middle [no match]

String with email at the end myMail@mail.com [no match]

myMail@mail.com [no match]

The regex I have managed to come up with so far is an inverse version of a popular email regex:

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

works for me only when I have just an email;

myMail@mail.com [no match]

but I need it to give me no match for ALL the examples containing an email. Any ideas how it can be done? Thanks

edited:

Found the answer by looking at

Regular expression to match a line that doesn't contain a word?

The working line is:

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

Thanks

Stempler
  • 1,309
  • 13
  • 25
  • How about `!line.matches("\\w+@\\w+\\.\\w+")`, or something similar? – Tim Biegeleisen Nov 27 '17 at 10:06
  • You should add `.*` before the email pattern in the lookahead, and perhaps, add a DOTALL modifier to ensure you check for the email on any line: `"(?s)^(?!.*[_A-Za-z0-9+-]+(?:\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(?:\\.[A-Za-z0-9]+)*(?:\\.[A-Za-z]{2,})$).*$"` – Wiktor Stribiżew Nov 27 '17 at 10:07

0 Answers0