0

I want to match everything except “eps1@“ within this string:

HelpMe_pleas3_eps1@

I tried [^eps1@] but this matched everything except every instance of “e” “p” “s” “1” “@“. I just want “eps1@“ excluded.

logi-kal
  • 7,107
  • 6
  • 31
  • 43
TR79
  • 19
  • 2

1 Answers1

1

There are many ways of doing it, depending on what you really need.

Using (.*)eps1@ you match everything before "eps1@" in the first capturing group. See demo #1.

Using (.*)eps1@(.*) you match everything before and after "eps1@" in the first and second capturing groups. See demo #2.

If "eps1@" occurs multiple times, you can use this regex:

(?:[^eps1@]|e(?!ps1@)|(?<!e)p(?!s1@)|(?<!ep)s(?!1@)|(?<!eps)1(?!@)|(?<!eps1)@)+

That means:

  • [^eps1@] any character but "e", "p", "s", "1", and "@"; OR
  • e(?!ps1@) the character "e" if not followed by "ps1@"; OR
  • (?<!e)p(?!s1@) the character "p" if not preceded by "e" and not followed by "s1@"; OR
  • and so forth.

See demo #3.

But have you considered just replacing "eps1@" with "" into your source string?

P.S. For the next questions I'd suggest you to be more precise on the language you're using.

logi-kal
  • 7,107
  • 6
  • 31
  • 43
  • Thanks!! Sorry I wasn't clear. Ultimately, I wanted to be able to extract/match "HelpMe_please3_" so that I can replace "eps1!@" with something else. I was able to achieve this by using `(.*)[^eps1@]`. – TR79 Sep 15 '20 at 17:00