Search code examples
regexpcre

Match all regex patterns until a delimiting pattern is reached


I have the following regex expression:

\w{6,}(?=(\s|\n)*hello there)

I am trying to match all patterns until the string "hello there" is reached.

ASD abc1234 
fegds abc12345 
xyz456 GD (jdkjf)
xyz1234 GD (jdkjf)
(jsdfk) def123 kjfg abc493 

hello there

kjfg feg4493

It only seems to match "abc493" whereas it should really match all 6 patterns: "abc1234", "abc12345", "xyz456", "xyz1234", "def123", and "abc493"

How do I modify the above regex expression to get the correct result?


Solution

  • In PCRE you could use backtracking verbs instead of lookaheads, which in this scenario performs better:

    (?s)hello there.*(*SKIP)(*F)|\w{6,}
    

    See live demo here