Home > database >  a regex to match a pattern between two specific strings
a regex to match a pattern between two specific strings

Time:07-02

let's say I've a text like the following:

THINGS H X F N AB end STUFF H X4 O 9D I,8U JK(S) XD LMJIK END OTHER_STUFF ....... end

I want to write a regex to get a the words between STUFF and the next following END (or OTHER_STUFF, in case end is not found):

H X4 O 9D I,8U JK(S) XD LMJIK

I tried following regex, but I couldn't make it stop after finding the first end, demo:

(?:\G(?!^)|STUFF)\s \K[a-z0-9(),]{1,16}(?=.*?end)

I would be happy to hear your suggestions.

CodePudding user response:

You must make sure [a-z0-9(),]{1,16} does not match end:

(?:\G(?!^)|STUFF)\s \K(?:(?!end)[a-z0-9(),]){1,16}(?=.*?end)

See the regex demo.

Here, the (?:(?!end)[a-z0-9(),]){1,16} part matches any lowercase letter, digit, (, ) or ,, one to 16 times, as many as possible, that does not start an end char sequence.

See more details about tempered greedy token here.

  • Related