Posted by Michael Fesser on 12/17/07 16:45
..oO(Patrick Drouin)
>I am puzzled at PHP's handling of regex. Here's the code:
>
><?php
>
>$str="aabcc";
>$pattern="/((a+)b?(c+))/";
>
>preg_match_all($pattern,$str,$matches);
>print_r($matches[0]);
>
>?>
>
>The behaviour I expect from the above would be to match:
>a
>aa
>c
>cc
>abc
>aabc
>abcc
>aabbcc
Nope. What's returned is the entire matched string and all parenthesized
sub strings (if there are any), but not every single matching point from
during the execution.
>The output is ALWAYS the maximum strings :
Correct.
>Array
>(
> [0] => Array
> (
> [0] => aabcc
> )
>
> [1] => Array
> (
> [0] => aabcc
> )
>
> [2] => Array
> (
> [0] => aa
> )
>
> [3] => Array
> (
> [0] => cc
> )
>
>)
>
>
>Any idea why the substrings are not picked up?
The above is exactly what you told preg_match() to return:
0: the entire matched string
1: the first sub pattern: ((a+)b?(c+)) => the entire string again
2: the second sub pattern: (a+) => aa
3: the third sub pattern: (c+) => cc
Micha
Navigation:
[Reply to this message]
|