Wrong negative look behind expression?

73 views Asked by At

I'm trying to filter every string which are not valid (simple) email adress. Thus starting with a dot is a "no go".

But my expression doesn't reject strings like [email protected]

preg_match('/^(?<!\.)[\w0-9\.]+(?<!\.)@+(?<!\.)[\w0-9\.]+\.\w{1,3}(\s*<[^>]+>)?$/', $part)

Shouldn't /^(?<!\.) do the trick?

EDIT: This question is not a duplicate of the indicated question.

I am not asking about the context, I want more information about look behind, the approach itself, not the specific use-case.

1

There are 1 answers

1
Dmitry Egorov On

It turns out its look-ahead counterpart (^(?!\.)) does the trick instead: https://regex101.com/r/zZ8yT7/1