Trent Renshaw Trent Renshaw - 7 months ago 47
PHP Question

PCRE match after (lookbehind) with capture groups delimited by slashes and end of line

My goal is a regex that captures {type} and {code} after propertyComplexity in the following string:-


{type} and {code} are variables, could be anything or nothing e.g.


I have started with the following expression:-


But this only captures {type} between the 2 slashes; the end delimiter for the capture groups needs to be slash or end of line.

The regex needs to capture all words after slash after propertyComplexity until terminated by end of line. For example:


Should yield 4 matches / capture groups; {type}, {code}, {param3}, {param4}

If it helps, this in the context of processing @path attributes in a WADL. The contents of the capture is actually irrelevant as it is the match count that will be used (in a comparison to the count of passed arguments in determining which WADL resource to call).

URL validity is not a requirement.

Matching on or after slash is not necessary when parameters in WADLs will always be encapsulated in {param} placeholders.

So I am simply using the following expression in a preg_match_all() == count($this->passedArgs)


Thank you everyone who contributed. Answer is being awarded to Jaytea.

Answer Source

You can use preg_match_all combined with the following expression to achieve what you want:


The '\G' assertion matches at the first position in the subject, which in this case effectively means that it matches at the position where the last match ended. The basic strategy here is to validate the format of the string at the beginning, then capture properties one at a time in subsequent rounds.

I'm not sure how strict you want to be with your validation, so I kept it quite simple.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download