Reputation: 8607
I'm looking for a PCRE pattern that would match the text in between the delimiters of any valid PCRE pattern regardless of delimiters and modifiers used.
Upvotes: 0
Views: 141
Reputation: 44259
There are four paired delimiters, as far as I know: ()
, []
, {}
, <>
. All other allowed characters are just used twice. According to the documentation, we can use any non-alphanumeric, non-whitespace, non-backslash character. So this pattern should work:
/
^
(?=([^a-zA-Z0-9\s\\\\])) # make sure the pattern begins with a valid delimiter
# and capture it into group 1
(?| # alternation for different delimiter types
# each alternative captures the pattern into group 2
\((.*)\) # handle (...)
|
\[(.*)\] # handle [...]
|
\{(.*)\} # handle {...}
|
<(.*)> # handle <...>
|
.(.*)\1 # handle all other delimiters with a backreference
)
[imsxeADSUXu]* # allow for modifiers
$
/xs
If you use this $pattern
in
preg_match($pattern, $input, $matches);
then you'll find your desired result in $matches[2]
.
Of course, this will accept a bunch of invalid patterns, because it does not ensure that the delimiter does not appear somewhere sooner in the pattern.
Upvotes: 2