Regex help

Regex help

am 17.12.2007 17:03:19 von MD Websunlimited

Hello everyone,

I am puzzled at PHP's handling of regex. Here's the code:


$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

The output is ALWAYS the maximum strings :

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?

Thanks
Patrick

Re: Regex help

am 17.12.2007 17:45:52 von Michael Fesser

..oO(Patrick Drouin)

>I am puzzled at PHP's handling of regex. Here's the code:
>
> >
>$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

Re: Regex help

am 17.12.2007 20:52:41 von MD Websunlimited

Hello Michael,

> 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 above is exactly what you told preg_match() to return:

Well OK, let me rephrase then, how can I tell PHP to match the
substrings. In my mind, (a+) means a, aa, aaa, ... and not only the
maximum string. I don't see how that behaviour is logical in any way.

Thanks,
Patrick

Re: Regex help

am 21.12.2007 18:40:58 von AnrDaemon

Greetings, Moi.
In reply to Your message dated Tuesday, December 18, 2007, 17:29:22,

> Hello,

> On 18 déc, 05:34, Toby A Inkster
> wrote:
>> Patrick Drouin wrote:
>> > $pattern="/((a+)b?(c+))/";
>>
>> $pattern="/(a+)b?(c+)/";

> If you try this, you will see that it spit out

> [0] => aaabccc
> [0] => aaa
> [0] => ccc

TH, it is

[0] => aaabccc
[1] => aaa
[2] => ccc

which is HIGHLY different.

> That's not what I'm looking for...

RTFM FTW.
In (0) it always return the whole matched [sub]string.
Just ignore the [0] entry if You do not want to deal with it.


--
Sincerely Yours, AnrDaemon