Negate the output of the regex pattern

91 views Asked by At

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

Sample String:

SMTP:[email protected];smtp:[email protected];sip:[email protected]

I have written a below regex which gives output as- [email protected]

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

Now i want everything except the above outcome i.e

SMTP:;smtp:[email protected];sip:[email protected]

I am trying to negate but it is not working.

Any help is much appreciated.

1

There are 1 answers

1
rikyeah On BEST ANSWER

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:

  • SMTP:
  • anything after a ; up until another SMTP
  • anything at the beginning of a line (no need of ;) up until another SMTP

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.