Search code examples
regexregex-lookarounds

Regex look ahead matching to greedy backwords


I am trying to match only paragraph indexes where the "TEST" is inside but my regex is matching also paragraph without it because there is "TEST" in the next one.

Can you help me and elaborate how to, in general, match only first occurence of pattern BEFORE some other patter?

asdasdasda
2.1 adasdasdasdasdwvwetwevtwtv
wetvwetv TEST wqrqwvrqw
qwvrqwvqwr
2.2 whergtvwe
wetvwetvwetveatw
evtwet
2.3 eyrnenytunrunert
vqevrerwv TEST aevtawtvwetv

^(\d+.\d+)(?=.*?TEST)


Solution

  • This regex only matches characters that are not followed by \d\.\d: demo

    ^(\d+\.\d+)(?=(?:.(?!\d\.\d))*TEST)
    

    Also the period between the numbers must be escaped if you want it to only match a period instead of being a wildcard.