Search code examples
c#regexlookbehind

Matching an email address only if a known prefix isn't present


I'm writing a regex expression to match email addresses, but only if a certain prefix is not present before the email. (This seems to apply to any non-literal string matcher after negative lookbehind.)

I have tried using negative lookbehind, but only successfully managed to not-match the first character of the email address if the prefix is present.

(?<!From: )([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)

First of all, don't mind the email address matcher, it's crude but works in my use case, and this question could apply to any non-literal string -matcher, replacing the capture group with literals works as intended.

What I wanted it to match are email addresses, if the prefix "From: " is not present in front of it.

This should not match the address

18.4.2019 9:35:02.115(22)->:From: address@subdomain.domain.tld

This should match the address

RCPT TO:<address@domain.tld>

What the regex I tried does, it matches correctly the email addresses without the prefix, but in the prefix case it matches them too, apart from the first letter which apparently is the only one matching the negative lookbehind. It does make sense.

Is there a way to get the negative lookbehind to apply to the whole capture group? Or should this be approached with a different kind of expression?


Solution

  • In regex engine in C# (also in other engines) non-fixed lenght lookbehinds are allowed, so you just need to modify your regex a little, alter your lookbehind:

    (?<!From: .*)
    

    Demo