php - The capturing group in the regular expression isn't output -
& lt ;? Php $ string = 'This is my regular expression'; $ Array = array (); Preg_match ('/^.* (my)) Regular (expression)?) $ / I', $ string, $ array); var_dump ($ array); ? & Gt; After the execution of this script I have:
array (size = 4) 0 = & gt; String 'This is my regular expression' (length = 29) 1 = & gt; String 'regular expression' (length = 19) 2 = & gt; String '' (length = 0) 3 = & gt; String 'expression' (length = 10) Why does not this capturing group (mine)?
The reason for this is that you have a greedy quantifier . Before this you instead of the following kind of a non-greedy quantifier * . should use: & lt ;? Php $ string = 'This is my regular expression'; $ Array = array (); Preg_match ('/^.*? ((My)? Regular (expression)?) $ / I', $ string, $ array); var_dump ($ array); ? & Gt;
[OUTPUT] array (size = 4) 0 = & gt; String 'This is my regular expression' (length = 29) 1 = & gt; String 'my regular expression' (length = 21) 2 = & gt; String 'Mary' (length = 2) 3 = & gt; String 'expression' (length = 10)
Comments
Post a Comment