regexnegate

Negate the output of the regex pattern


I want to negate the outcome of the regex pattern, so that it should return everything except the regex outcome.

Sample String:

SMTP:test.abc@xyz.com;smtp:test.123@xyz.biz;sip:test.123@xyz.biz

I have written a below regex which gives output as- test.abc@xyz.com

(?<=SMTP:)(.*?)(?=;)

Now i want everything except the above outcome i.e

SMTP:;smtp:test.abc@xyz.biz;sip:test.abc@xyz.biz

I am trying to negate but it is not working.

Any help is much appreciated.


Solution

  • It might be overcomplicated, but if you need multiline matching and a smtp account can be in the beginning of a line, this:

    (SMTP:)|(;[^(SMTP)]*)|(^[^(SMTP)]*)
    

    would match:

    Have a look at some tests here.

    This can break down if an email name contains SMTP in it, but I hope you won t have any.

    Another approach is use your matching regex, (?<=\SMTP:)(.*?)(?=\;), and keep deleting what it matches from the string.