TAGS :Viewed: 4 - Published at: a few seconds ago

[ Regex issue with capturing group PHP ]

I have been looking around for a while and haven't been able to find what I'm looking for or perhaps I just don't know enough to know what I am looking for...

I have a situation where I want to capture and expression surrounded by parentheses that is a alphabetic character between 1 and 5 characters long. This is not difficult. Next I want to exclude the exact string (AP) from my search.

I am using regex101 and I appear to be getting a match on the string I want to get (or the space right before it) but the match is only returning ''' and not the full (EXC) that I want. Here is the regex I have currently:


Any suggestions or pointers in the right direction; I will provide more information if necessary.

Answer 1

Here is a comparison of the regex you first had, and the regex that works (spaces added):

/((?= \(\D{1,5}\) )) (?:  .(?!AP ))/gism
/((     \D{1,5}   )) (?<!  (  AP ))/gism

Your first pattern will match the opening parenthesis when they are around something nonnumeric that doesn't start with ap. Look arounds do not match characters, keep in mind. (The dot is the only character that isn't in a look around.)

The other pattern removes the literal parenthesis: \( and \). It also removed the look ahead ?= so that you are actually capturing something. The last part of the regex is a negative look behind. In this case, all it does is prevent the pattern from matching a p when the thing it matches ends in ap.

I cannot explain why the second pattern has so many unnecessary parenthesis, however. This is equivalent: