If you are trying to parse single or double quoted strings, it should be done
in two steps. Validation, then parse for values.
You could probably do both in a single regex with the use of a \G
anchor,
validating with \A\G
and parsing with just the \G
.
If you are sure its valid, you can skip the validation.
Below are the two parts (can be combined if needed).
Note that it parses the single or double quotes using the un-rolled loop method,
which is pretty quick.
Validation:
# Validation: '~^(?s)[^"\']*(?:"[^"\\\]*(?:\\\.[^"\\\]*)*"|\'[^\'\\\]*(?:\\\.[^\'\\\]*)*\'|[^"\'])*$~'
^
(?s)
[^"']*
(?:
"
[^"\\]*
(?: \\ . [^"\\]* )*
"
|
'
[^'\\]*
(?: \\ . [^'\\]* )*
'
|
[^"']
)*
$
Parsing:
# Parsing: '~(?s)(?|"([^"\\\]*(?:\\\.[^"\\\]*)*)"|\'([^\'\\\]*(?:\\\.[^\'\\\]*)*)\')~'
(?s) # Dot all modifier
(?| # Branch Reset
"
( # (1), double quoted string data
[^"\\]*
(?: \\ . [^"\\]* )*
)
"
| # OR
'
( # (1), single quoted string data
[^'\\]*
(?: \\ . [^'\\]* )*
)
'
)
solved Capturing all method arguments default values