Professional Web Applications Themes

A small regex question - PHP Development

Hello, This code: <?php preg_replace_callback("/(a)|(b)/", create_function('$x', 'foreach ($x as $y => $z)'. 'echo "\\$y: $z<br>";'), "b"); ?> Outputs:

Thread: A small regex question

: b
: : b I expect 'b' to be placed in
, but for some reason
contains a null value and 'b' is moved to . Can someone please explain why it happens? Thank you...

  1. #1

    Default A small regex question

    Hello,

    This code:

    <?php
    preg_replace_callback("/(a)|(b)/",
    create_function('$x',
    'foreach ($x as $y => $z)'.
    'echo "\\\$y: $z<br>";'),
    "b");
    ?>

    Outputs:

    \0: b
    \1:
    \2: b

    I expect 'b' to be placed in \1, but for some reason \1 contains a
    null value and 'b' is moved to \2. Can someone please explain why it
    happens?

    Thank you
    Online Guest

  2. #2

    Default Re: A small regex question

    On 29 Oct 2003 16:00:25 -0800, [email]online131hotmail.com[/email] (Online) wrote:
    >This code:
    >
    > <?php
    > preg_replace_callback("/(a)|(b)/",
    > create_function('$x',
    > 'foreach ($x as $y => $z)'.
    > 'echo "\\\$y: $z<br>";'),
    > "b");
    > ?>
    >
    >Outputs:
    >
    > \0: b
    > \1:
    > \2: b
    >
    >I expect 'b' to be placed in \1, but for some reason \1 contains a
    >null value and 'b' is moved to \2. Can someone please explain why it
    >happens?
    You have two capturing subexpressions; 'b' is in the second one.

    Did you mean (a|b) instead?

    --
    Andy Hassall (andyandyh.co.uk) icq(5747695) ([url]http://www.andyh.co.uk[/url])
    Space: disk usage ysis tool ([url]http://www.andyhsoftware.co.uk/space[/url])
    Andy Hassall Guest

  3. #3

    Default Re: A small regex question

    > You have two capturing subexpressions; 'b' is in the second one.
    >
    > Did you mean (a|b) instead?
    Yea, I have two capturing brackets, but the pattern is separated by |
    into two parts - (a) and (b).

    Afaik when (a) fails to match, the regex engine should completely
    ignore the rest of the current part and move on to (b) and try to
    match it against the text. And if (b) matched, the letter 'b' should
    be placed inside \1.

    The problem here is that \1 gets a null value while 'b' is pushed to
    \2, and I don't understand why it acts this way. On mIRC, that
    includes an implementation of PCRE, the letter 'b' is placed in \1 (as
    I expect), though on PHP it's moved to \2.

    I am aware there are other ways of doing this simple match, like
    "(a|b)" or "([ab])", but this problem occured to me with a more
    complicated pattern and I think "(a)|(b)" is the simplest form of
    presenting it.
    Online Guest

  4. #4

    Default Re: A small regex question

    Online wrote:
    > Yea, I have two capturing brackets, but the pattern is separated by |
    > into two parts - (a) and (b).
    >
    > Afaik when (a) fails to match, the regex engine should completely
    > ignore the rest of the current part and move on to (b) and try to
    > match it against the text. And if (b) matched, the letter 'b' should
    > be placed inside \1.
    No. With a pattern like "(a)|(b)" matching against a string like "b",
    $0 contains the entire pattern match; $1 contains the text matched by
    the first capturing subpattern (nothing); and $2 contains the text
    matched by the second capturing subpattern ("b").

    The description of preg_replace's replacement parameter might be
    helpful in understanding what's really happening:

    | Replacement may contain references of the form \\n or (since PHP
    | 4.0.4) $n, with the latter form being the preferred one. Every such
    | reference will be replaced by the text captured by the n'th
    | parenthesized pattern. n can be from 0 to 99, and \\0 or $0 refers
    | to the text matched by the whole pattern. Opening parentheses are
    | counted from left to right (starting from 1) to obtain the number
    | of the capturing subpattern.

    [url]http://www.php.net/manual/en/function.preg-replace.php[/url]
    > I am aware there are other ways of doing this simple match, like
    > "(a|b)" or "([ab])", but this problem occured to me with a more
    > complicated pattern and I think "(a)|(b)" is the simplest form of
    > presenting it.
    Those are different to your first pattern. Which do you really want?

    --
    Jock
    John Dunlop Guest

Similar Threads

  1. regex question
    By Math55 in forum PERL Beginners
    Replies: 19
    Last Post: July 21st, 06:22 PM
  2. perl regex question
    By Chad Williams in forum PERL Miscellaneous
    Replies: 7
    Last Post: September 11th, 07:04 AM
  3. regex conditional question
    By Jonathan Driller in forum PERL Miscellaneous
    Replies: 5
    Last Post: August 19th, 09:59 PM
  4. Simple regex question
    By Gunnar Hjalmarsson in forum PHP Development
    Replies: 9
    Last Post: August 15th, 12:35 AM
  5. Regex question...
    By Greg Bacon in forum PERL Miscellaneous
    Replies: 1
    Last Post: July 15th, 09:21 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •