tl;dr:
I am searching for a way to match the closing character sequence based upon the style of the opening sequence syntax in PHP with PCRE-style regular expressions.
The task
I am writing a module to capture all translatable strings from written PHP code. One responsibility of this module will be to also capture any translation context stated within the code. This context is provided as part of an options array.
In PHP (afair starting with version 5.4), there are two different styles possible to define an array:
a) array(...)
b) [...]
I now want to write a regular expression that is able to recognize both styles. The pattern should be able to correctly match the ending character sequence depending on the style chosen to start the array.
Unfortunately, I was not able to find any documentation on how to apply the IF-statement to a given capturing group.
In theory it should look something like this:
/ ... (array\(|\[) ... (?(?=\1==\[)\]|\)) ... /
(Note: "..." in the line above should indicate that the regex pattern is longer than stated here. This should only serve as an example for what I am trying to achieve)
The (?(?=\1==\[)\]|\))part translated to "normal language": If the contents of the first capturing group is an opening square bracket, then the pattern should capture a closing square bracket, otherwise a closing round bracket is required.
Is it possible to achieve something like this? Any help is greatly appreciated!
Thanks in advance
Chris