You are here: Re: PCRE sub-expressions matching « PHP Programming Language « IT news, forums, messages
Re: PCRE sub-expressions matching

Posted by Jerry Stuckle on 10/31/05 22:36

jens@aggergren.dk wrote:
> I was wondering if anyone could tell be whether it's possible to
> instruct preg_match not to save subexpressions matches under 0, 1...n
> keys when using named subexpressions; I.e instruct preg_named to
> matched under their names only,
> and not under their numbers. E.g. take the following example:
> <pre style="width:100%;text-align:justify">
> /^(?:(?:ftp|http)\:\/\/)?(?P<host>[^\/\:]*)(?:\:(?P<port>[0-9]{4}))?(?:(?P<path>\/(?:[~\%\w\.\-_]*\/)+)?(?P<file>[~\%\w\.\-_]+)(?:\?(?P<query>.*))?)?$/
> </pre>
> for the string
> "http://groups.google.com/group/comp.lang.php/post?hl=en&_done=%2Fgroup%2Fcomp.lang.php%2Fbrowse_thread%2Fthread%2Fa56103941c7e5176%2F9b7c8045d2841990%3Fq%3Dregular+expressions+sub-expressions%26rnum%3D1%26hl%3Den%26&"
> generates the following matches:
>
> Array
> (
> [0] =>
> http://groups.google.com/group/comp.lang.php/post?hl=en&_done=%2Fgroup%2Fcomp.lang.php%2Fbrowse_thread%2Fthread%2Fa56103941c7e5176%2F9b7c8045d2841990%3Fq%3Dregular+expressions+sub-expressions%26rnum%3D1%26hl%3Den%26&
> [host] => groups.google.com
> [1] => groups.google.com
> [port] =>
> [2] =>
> [path] => /group/comp.lang.php/
> [3] => /group/comp.lang.php/
> [file] => post
> [4] => post
> [query] =>
> hl=en&_done=%2Fgroup%2Fcomp.lang.php%2Fbrowse_thread%2Fthread%2Fa56103941c7e5176%2F9b7c8045d2841990%3Fq%3Dregular+expressions+sub-expressions%26rnum%3D1%26hl%3Den%26&
> [5] =>
> hl=en&_done=%2Fgroup%2Fcomp.lang.php%2Fbrowse_thread%2Fthread%2Fa56103941c7e5176%2F9b7c8045d2841990%3Fq%3Dregular+expressions+sub-expressions%26rnum%3D1%26hl%3Den%26&
> )
>
> Notice that all subexpressions are saved twice in the array. Can this
> be avoided?
>

No, it's not saving it twice in the array. Look at your indicies.

When PHP has a non-numeric index [i.e. 'host'], it also reserves a
numeric index for the item (i.e. 1).

When you use print_r to print the array, you see both the non-numeric
and numeric indicies.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

 

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

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