PCRE sub-expressions matching

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jens@aggergren.dk

    #1

    PCRE sub-expressions matching

    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:10 0%;text-align:justify">
    /^(?:(?:ftp|http )\:\/\/)?(?P<host>[^\/\:]*)(?:\:(?P<port >[0-9]{4}))?(?:(?P<pa th>\/(?:[~\%\w\.\-_]*\/)+)?(?P<file>[~\%\w\.\-_]+)(?:\?(?P<quer y>.*))?)?$/
    </pre>
    for the string
    "http://groups.google.c om/group/comp.lang.php/post?hl=en&_don e=%2Fgroup%2Fco mp.lang.php%2Fb rowse_thread%2F thread%2Fa56103 941c7e5176%2F9b 7c8045d2841990% 3Fq%3Dregular+e xpressions+sub-expressions%26r num%3D1%26hl%3D en%26&"
    generates the following matches:

    Array
    (
    [0] =>

    [host] => groups.google.c om
    [1] => groups.google.c om
    [port] =>
    [2] =>
    [path] => /group/comp.lang.php/
    [3] => /group/comp.lang.php/
    [file] => post
    [4] => post
    [query] =>
    hl=en&_done=%2F group%2Fcomp.la ng.php%2Fbrowse _thread%2Fthrea d%2Fa56103941c7 e5176%2F9b7c804 5d2841990%3Fq%3 Dregular+expres sions+sub-expressions%26r num%3D1%26hl%3D en%26&
    [5] =>
    hl=en&_done=%2F group%2Fcomp.la ng.php%2Fbrowse _thread%2Fthrea d%2Fa56103941c7 e5176%2F9b7c804 5d2841990%3Fq%3 Dregular+expres sions+sub-expressions%26r num%3D1%26hl%3D en%26&
    )

    Notice that all subexpressions are saved twice in the array. Can this
    be avoided?

  • Joshie Surber

    #2
    Re: PCRE sub-expressions matching

    > Notice that all subexpressions are saved twice in the array. Can this[color=blue]
    > be avoided?[/color]

    Without trying it (never knew you could name them <sheepish grin>) I
    would assume you could use (?:P<host>

    On the other hand, however, does it really matter? Even if you are
    looping, just do a quick test to see if the key is numeric

    Also, the url_parse function does what you are looking for, and will do
    it faster.

    Comment

    • jens@aggergren.dk

      #3
      Re: PCRE sub-expressions matching

      >Without trying it (never knew you could name them <sheepish grin>) I[color=blue]
      >would assume you could use (?:P<host>[/color]
      Sadly not, because ?: effectively instruct the parser not to save the
      subexpression match, so using it with the P option simply causes an
      error (or nothing to be mached).
      [color=blue]
      >On the other hand, however, does it really matter? Even if you are
      >ooping, just do a quick test to see if the key is numeric[/color]
      Sure, and i realize that it's probably not something I should worry
      about until I sit down and do a real benchmark. Premature optimization
      is an antipattern after all.
      [color=blue]
      >Also, the url_parse function does what you are looking for, and will do
      >it faster.[/color]
      Granted, it only an example; Also i wanted slightly different behavior.

      Comment

      • Jerry Stuckle

        #4
        Re: PCRE sub-expressions matching

        jens@aggergren. dk wrote:[color=blue]
        > 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:10 0%;text-align:justify">
        > /^(?:(?:ftp|http )\:\/\/)?(?P<host>[^\/\:]*)(?:\:(?P<port >[0-9]{4}))?(?:(?P<pa th>\/(?:[~\%\w\.\-_]*\/)+)?(?P<file>[~\%\w\.\-_]+)(?:\?(?P<quer y>.*))?)?$/
        > </pre>
        > for the string
        > "http://groups.google.c om/group/comp.lang.php/post?hl=en&_don e=%2Fgroup%2Fco mp.lang.php%2Fb rowse_thread%2F thread%2Fa56103 941c7e5176%2F9b 7c8045d2841990% 3Fq%3Dregular+e xpressions+sub-expressions%26r num%3D1%26hl%3D en%26&"
        > generates the following matches:
        >
        > Array
        > (
        > [0] =>
        > http://groups.google.com/group/comp....%26hl%3Den%26&
        > [host] => groups.google.c om
        > [1] => groups.google.c om
        > [port] =>
        > [2] =>
        > [path] => /group/comp.lang.php/
        > [3] => /group/comp.lang.php/
        > [file] => post
        > [4] => post
        > [query] =>
        > hl=en&_done=%2F group%2Fcomp.la ng.php%2Fbrowse _thread%2Fthrea d%2Fa56103941c7 e5176%2F9b7c804 5d2841990%3Fq%3 Dregular+expres sions+sub-expressions%26r num%3D1%26hl%3D en%26&
        > [5] =>
        > hl=en&_done=%2F group%2Fcomp.la ng.php%2Fbrowse _thread%2Fthrea d%2Fa56103941c7 e5176%2F9b7c804 5d2841990%3Fq%3 Dregular+expres sions+sub-expressions%26r num%3D1%26hl%3D en%26&
        > )
        >
        > Notice that all subexpressions are saved twice in the array. Can this
        > be avoided?
        >[/color]

        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@attgl obal.net
        =============== ===

        Comment

        • jens@aggergren.dk

          #5
          Re: PCRE sub-expressions matching

          >When PHP has a non-numeric index [i.e. 'host'], it also reserves a[color=blue]
          >numeric index for the item (i.e. 1).[/color]
          That is a complete myth(or at least not the case in never versions of
          .....true many functions that such as the mysql functions return value
          under both numeric and alphanumeric indexes... but it's not default
          behavior.

          e.g i set:
          $test = array();
          $test['foo'] = 'bar';

          var_dump($test[0]);

          See Also:

          [color=blue]
          >When you use print_r to print the array, you see both the non-numeric
          >and numeric indicies.[/color]
          only if both are explicitly set

          Comment

          • jens@aggergren.dk

            #6
            Re: PCRE sub-expressions matching

            Sorry, what i mean is:
            $test = array();
            $test['foo'] = 'bar';

            var_dump($test[0]);

            returns NULL

            Comment

            Working...