Reply to Re: Regex help

Your name:

Reply:


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

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация