Search code examples
phpregexnegative-lookbehind

php regex: Alternative to backreference in negative lookbehind


I want to find instances where a captured group does not appear later in the string:

aaaBbb  = CccBbb  <- format is valid, skip
aaaDddd = CccDddd <- format is valid, skip
aaaEeee = CccFfff <- format is not valid, match this one only

So this matches the lines I don't want to match ( https://regex101.com/r/lon87L/1 )

/^ +\w+([A-Z][a-z+]) += +\w+\1$/mg

I've read on https://www.regular-expressions.info/refadv.html that php doesn't support backreferences inside a negative lookbehind, but other implementations of regex can. So something like this would match the invalid lines that I want to match, but it doesn't work in php:

/^ +\w+([A-Z][a-z+]) += +\w+(?<!\1)$/mg

Is there anything else that would work, other than matching all of three lines and looping through the matches in a php foreach?


Solution

  • Try using using a negative lookahead instead of a negative lookbehind. It works equally well, plus it works in PHP.

    ^ +\w+([A-Z][a-z]+) += +(?!\w+\1).*$
    

    regex101 demo

    PHP demo