You are here: Re: expression between vertical bars « PHP « IT news, forums, messages
Re: expression between vertical bars

Posted by Matthew Weier O'Phinney on 10/19/91 11:15

* Jan <jjj@xs4all.nl>:
> Could someone explain to me the use of the vertical bar in
> expressions like "|<[^>]+>(.*)</[^>]+>|U". I understand that the
> vertical bars are used to separate the expression from the modifier,
> but can't find any documentation about the reasons and rules to do
> this.

This works in the PCRE functions, and has to do with the P in PCRE --
Perl. In perl, when performing a regexp, you can use whatever character
you desire to act as 'quotes' surrounding it. The typical one is a
forward slash, but when you are going to be matching forward slashes,
this can be inconvenient (the following examples are all perl, with a
PHP version follwing):

if ( /http:\/\/www.php.net\// ) # perl
if (preg_match('/http:\/\/www.php.net\//', $url)) # PHP

You get what's called 'leaning toothpick' syndrome. So, to make things
easier, you can specifiy a different regex delimiter:

if ( m|http://www.php.net| ) # perl
if (preg_match('|http://www.php.net|', $url) # PHP

(In perl, the 'm' indicates you're doing a match operation, and needs to
be present if you're using a delimiter other than a slash; it's not
necessary in PHP as it's implied.)

The modifier occurs after the delimiters -- and this can happen
regardless of delimiter:

if ( m|http://www.php.net|U ) # perl
if (preg_match('|http://www.php.net|U', $url) # PHP

if ( /http:\/\/www.php.net\//U ) # perl
if (preg_match('/http:\/\/www.php.net\//U', $url) # PHP

If you want more information on this, pick up a copy of "Mastering
Regular Expressions" or "Programming Perl", both from O'Reilly Press.

--
Matthew Weier O'Phinney | WEBSITES:
Webmaster and IT Specialist | http://www.garden.org
National Gardening Association | http://www.kidsgardening.com
802-863-5251 x156 | http://nationalgardenmonth.org
mailto:matthew@garden.org | http://vermontbotanical.org

 

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

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