1

Got another one for you here! I've been fiddling for a while but I'm getting lost. I think the maybe the key to acheiving this is in combining conditionals with positive lookaheads?

I want to always find the start of a string. I've called it "MatchMe!" in the example but in reality it can be anything. Part 1, Part 2, Part 3 and Part 4 are always the same (well not the same but I can regex these reliably)

Is there a way to try and do a positive lookahead with Part 1, if that doesn't exist, try a positive lookahead with Part 2, and if that doesn't exist try a positive lookahead with Part 3, and lastly try the same with Part 4? Or am I overcomplicating things

My lousy attempt is here but it gives an idea of my problem :-)

https://regex101.com/r/TvgXXB/1

Many thanks again!

UnluckyForSome9
  • 301
  • 1
  • 9

1 Answers1

2

You may use

^.*?(?=\s*(?:Part1|Part2|Part3|Part4))

See the regex demo

Details

  • ^ - start of string
  • .*? - any 0+ chars, other than line break chars, as few as possible
  • (?=\s*(?:Part1|Part2|Part3|Part4)) - a location in string that, immediately to the right of it, is followed with
    • \s* - 0+ whitespaces
    • (?:Part1|Part2|Part3|Part4) - either of the alternatives listed in the non-capturing group.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563