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

PCRE sub-expressions matching

From: <jens@aggergren.dk>
Date: Mon Oct 31 2005 - 13:48:59 CET

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