1

I want to make a regex to capture a string of format test1 anything here or anything here test2, but not test1 anything here test2.

Is there a way to write a regex for this? Basically, I want something like /^(test1 )?(.*)$( test2)?/i, but preventing test1 and test2 from both appearing. Also, my example is wrong because it never gets past the 2nd capturing group... How can I do this?

Edit: I want to be able to capture the anything here part, so it needs to be inside its own capturing group.

Tirafesi
  • 1,297
  • 2
  • 18
  • 36

1 Answers1

1

You can use alternative of negative lookahead and lookbehind:

(test1 (((?!test2).)*)$)|(^(((?<!test1).)*)test2)

Demo

Explaination:

  • (test1 ((?!test2).)*$) find lines with test1 and no test2:
    • test1 matches text test1
    • (?!test2) negative lookahead - match place unless there is test2 ahead
    • ((?!test2).)* is a tempered greedy token (matching as much as possible until test2 text)
    • $ end-of-line assertion to assure there is no tailingtest2

The second part of the alternative is similar, but we check there is no leading test1 using negative lookbehind (?<!...)

Worth reading: https://stackoverflow.com/a/37343088/580346

mrzasa
  • 22,895
  • 11
  • 56
  • 94
  • Thanks! I forgot about something that I needed though... Could you please see the edit I did to the question? – Tirafesi Mar 01 '18 at 14:03
  • Updated (added groups). Unfortunately it will not work in JS (lookbehind is not supported). I'll update when I find a better idea. – mrzasa Mar 01 '18 at 14:57