1

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.

bobble bubble
  • 16,888
  • 3
  • 27
  • 46
user2223987
  • 91
  • 1
  • 6
  • What regex tool or language are you using here? – Tim Biegeleisen Jan 27 '22 at 08:22
  • Simply replace with an empty string – Wiktor Stribiżew Jan 27 '22 at 08:30
  • Ok, so let me explain more on this. I have to pass it on as json request which has predfined API with two required values- regex-"" replace-" Basically, i want to return the final string as "test.abc@xyz.com" so i am working in reverse, like finding this pattern and then negating it so that i can get the rest and then i will replace that with "". So at the end i will have "test.abc@xyz.com". In other words, i have to write the regex for "SMTP:;smtp:test.abc@xyz.biz;sip:test.abc@xyz.biz" – user2223987 Jan 27 '22 at 08:33
  • Just matching [`^SMTP:|;.+`](https://regex101.com/r/zIgkXe/1) won't suffice, would it? – bobble bubble Jan 27 '22 at 09:10
  • It would, but it will fail if the position of the "SMTP" changes..right? smtp:test.abc@xyz.biz;SMTP:test.abc@xyz.com;sip:test.abc@xyz.biz – user2223987 Jan 27 '22 at 10:06
  • @user2223987 yes, it would fail if the data is not following the pattern... – bobble bubble Jan 27 '22 at 11:04
  • Can't you just search for eg `.*?\bSMTP:(.*?);.*` and replace with captured `$1` like someone else already mentioned? See [this demo at regex101](https://regex101.com/r/aixJoz/1). – bobble bubble Jan 27 '22 at 11:13
  • As per your selected answer, here another idea: [`SMTP:|(?:;|^)(?:(?!SMTP).)+`](https://regex101.com/r/OzKoq9/1) – bobble bubble Jan 29 '22 at 15:07

1 Answers1

1

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.

rikyeah
  • 1,896
  • 4
  • 11
  • 21
  • Just to mention that `[^(SMTP)]` [represents a character](https://www.regular-expressions.info/charclass.html) class. It matches any character, that is not in the list. In this case any character besides `^`,`(`,`S`,`M`,`T`,`P`,`)` which is probably not desired. Perhaps you were thinking of something like `(?:(?!SMTP).)`. – bobble bubble Jan 29 '22 at 15:17