0

I have some strings like the following:

abc4back
abc4backpre
abc4front
abc4frontpre
abc3side
abc3sidepre
xyz4over
xyz4overpre

and I want to get those only with the "abc4" but without the "pre". So far, my regex is:
abc4.*(?!pre).

But, when I ran this, I got the error:
error parsing regexp: invalid or unsupported Perl syntax: `(?!` .

I now know that this is because lookaheads are not supported in go.

But, I cannot figure out what expression I should use instead of ?!. Does anyone know what will work?

Flimzy
  • 60,850
  • 13
  • 104
  • 147
  • is `pre` always at the end of the string? – mjrezaee Jul 20 '20 at 21:57
  • Even if the regex engine supported lookarounds, your regex is wrong, you would need `abc4(?!.*pre)` or even `^abc4(?!.*pre$)`. You can re-write the lookahead using an alternation of negated character classes as shown [here](https://stackoverflow.com/a/37988661/3832970). – Wiktor Stribiżew Jul 20 '20 at 21:58

1 Answers1

0

you can do this by multi negated character as

^abc4(?:.*(?:[^p].{2}|p[^r].{1}|pr[^e])|.{0,2})$

Regex Demo

mjrezaee
  • 1,040
  • 2
  • 9