Reputation: 1048
i try to build a small system that catches variables from html template.
the variables defined as @XXX@ and may (but not required) to have extra parameters that are sent with colon (:), i.e. @XXX@:j to send the data as json encoded.
what i manage to do is to create a preg_match_all to capture the variables and those extra parameters. so i came up with this preg:
preg_match_all("/(@.*@(?:(j|n|x|z))?)/imU", $string,$this->localVariables,PREG_PATTERN_ORDER);
j|n|x|z are the avialiable extra parameters that are possible.
the string that i send to $string is: @geterr@ @domain@:j @jhon@:n
the result i get from preg_match_all is:
Array
(
[0] => Array
(
[0] => @geterr@
[1] => @domain@
[2] => @jhon@
)
[1] => Array
(
[0] => @geterr@
[1] => @domain@
[2] => @jhon@
)
[2] => Array
(
[0] =>
[1] =>
[2] =>
)
)
i know (or think i know) that ?: is used for optional sub pattern the modifires iv'e used are: i for case insensitive m for to allow my string to be multiline U - to be non greedy
i have no other clue what i do wrong.
any help shall be greatly appriciated
Upvotes: 2
Views: 9297
Reputation: 92976
There are some issues in your pattern /(@.*@(?:(j|n|x|z))?)/imU
You don't need a capturing group around the whole pattern.
?:
is creating a non capturing group, it is not limited to optional groups.
The modifier m
is called multiline
, but this is a bit misleading it affects only the anchors ^
and $
to match also the start and end of a row and not only of the string.
What you want is the modifier s
, the singleline
modifier. It treats the whole string as one line, and affects the .
to match also newline characters.
The modifier U
makes your whole regex ungreedy. This is not what you want, because it affects also your optional group and because it is at the end of the pattern it will never match.
You need to match the :
in your string
So I would remove U
and make only the first quantifier ungreedy, by adding a ?
after it.
So I think your regex should be:
/@(.*?)@(?::(j|n|x|z))?/is
This would put the first part between the @
in the first capturing group and the parameter in the second group.
See it here on Regexr
Upvotes: 4