Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression that matches everything EXCEPT email addresses

Tags:

regex

Assume this is the input:

This is a paragraph This is a paragraph This is a paragraph This is a paragraph This is a paragraph This is an email address, [email protected]

I want a regex that matches everything EXCEPT the email address.

I have tried:

^((?!@).)*$

This matches everything except the LINE that the email is on. I need it to match all those words too. So the only thing that is not matched is [email protected].

like image 786
kyle Avatar asked Jan 01 '26 11:01

kyle


1 Answers

First, define what an email address is. As has been mentioned, this is non-trivial. That said, you can probably simplify it.

For example, let's say we consider any run of characters containing at least one @ symbol to be an email address. Then we can use:

(^|\s)[^@]+($|\s)

Note that we can't use \b (word boundary), since the @ itself is a word boundary, so you'd capture the parts of the email. Instead we jury rig our own definition of a word boundary to be a line beginning, line end, or white space.

The major problem with this approach is that you have the potential for false positives (or negatives, as the case may be); that is, any word with an @ will not be captured.

Depending on your needs, I suspect this will probably be sufficient.

like image 111
Asmor Avatar answered Jan 03 '26 02:01

Asmor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!