Reply to Re: Capturing substrings with preg_match_all()

Your name:

Reply:


Posted by gosha bine on 06/05/07 17:08

On 05.06.2007 17:18 ngmr80 wrote:
> Hi,
>
> I'm experiencing a problem when trying to capture substrings with
> preg_match_all() from strings like
>
> "set('Hello','World')"
>
> using the following Regular Expression (PERL syntax):
>
> "/^set\((?:'([^']+)'(?(?!\)),))+\)$/"
>
> The resulting matches array looks like this:
>
> Array
> (
> [0] => Array
> (
> [0] => set('Hello','World')
> )
>
> [1] => Array
> (
> [0] => World
> )
>
> )
>
> I wonder why "Hello" is not in the matches array. Did I do something
> wrong in my Regular Expression?
>
> Thanks in advance.
> Ingmar
>

Hi Ingmar

grouping is not automatic, to capture N strings you must have N groups
in your expression or use preg_match_all, for example:

$re1 = '/set\( (.*?) \)/x';
$re2 = "/' (?: \\\\. | [^'] )* ' /x";

preg_match($re1, "set('Hello','Wo\'rld')", $m);
preg_match_all($re2, $m[1], $m);
print_r($m);




--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok

[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

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