Reply to Re: Regex help

Your name:

Reply:


Posted by David Wahler on 02/03/06 20:59

Nicholas Sherlock wrote:
> Hey there,
>
> I'm trying to get a Perl regular expression to work, and I can't seem to
> get it quite right.
>
> It should match text like [download: 12,20,28] and match the subpatterns
> "12", ",20", ",28".
>
> /\[download:[^0-9]*([0-9]+)(,[0-9]+)*\]/i
>
> When I try it, it only returns the first and last items (Ie. "12",
> ",28"). What am I missing?

The details are in the reference
<http://us3.php.net/manual/en/reference.pcre.pattern.syntax.php>,
they're just not always easy to find.

The paragraph you're looking for is at the bottom of the "Repetition"
section:

(quote)
When a capturing subpattern is repeated, the value captured is the
substring that matched the final iteration. For example, after
(tweedle[dume]{3}\s*)+ has matched "tweedledum tweedledee" the value
of the captured substring is "tweedledee". However, if there are nested
capturing subpatterns, the corresponding captured values may have been
set in previous iterations. For example, after /(a|(b))+/ matches
"aba" the value of the second captured substring is "b".
(end quote)

So, it doesn't look like you can use regular expressions to split a
string like that. If you need to split based on a real regular
expression rather than a fixed string, check out pcre_split.

-- David

[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

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