You are here: Re: can't find the right PCRE pattern for a preg_match_all « PHP Programming Language « IT news, forums, messages
Re: can't find the right PCRE pattern for a preg_match_all

Posted by Rik on 06/06/06 16:15

greatprovider wrote:
> hah...that was the trick...thank you all...
>
> one last question...now would anyone mind explaining me how "$1"
> works?

In a regular expression, you can "capture" pieces that match a pattern with
(). The number after the $ indicates which piece, captures are numbered from
the first opening '('.

For instance:
**567HJK

'/((\*{2})(\d+))/'

$1 will contain the match: '**567';
$2 will contain the match: '**';
$3 will contain the match: '567';

Normally, pieces that have to match a certain regex, but aren't used any
further, don't need (). In some cases, it's necessary for the pattern. In
that case, you could just use the numbered matches you need, discarding the
others (with multiple captures in a regex, it is absolutely not necessary to
use them all). To keep a complex regex more clear, you could also make a
'non-capturing' group by adding ?: after the opening. For instance:
(?:\s+(\d+)) will capture the digits in $1, instead of $2, because the first
parenthesis is told not to capture anything.

Want to learn more about regexes?
http://www.regular-expressions.info/tutorialcnt.html was a big help for me.

Grtz,
--
Rik Wasmus

 

Navigation:

[Reply to this 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

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