Re: PCRE sub-expressions matching
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.php archive

Re: PCRE sub-expressions matching

From: Jerry Stuckle <jstucklex@attglobal.net>
Date: Mon Oct 31 2005 - 21:36:11 CET

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
==================
Received on Mon Nov 21 02:53:46 2005