Capturing substrings with preg_match_all()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ngmr80

    #1

    Capturing substrings with preg_match_all()

    Hi,

    I'm experiencing a problem when trying to capture substrings with
    preg_match_all( ) from strings like

    "set('Hello','W orld')"

    using the following Regular Expression (PERL syntax):

    "/^set\((?:'([^']+)'(?(?!\)),))+ \)$/"

    The resulting matches array looks like this:

    Array
    (
    [0] =Array
    (
    [0] =set('Hello','W orld')
    )

    [1] =Array
    (
    [0] =World
    )

    )

    I wonder why "Hello" is not in the matches array. Did I do something
    wrong in my Regular Expression?

    Thanks in advance.
    Ingmar

  • gosha bine

    #2
    Re: Capturing substrings with preg_match_all( )

    On 05.06.2007 17:18 ngmr80 wrote:
    Hi,
    >
    I'm experiencing a problem when trying to capture substrings with
    preg_match_all( ) from strings like
    >
    "set('Hello','W orld')"
    >
    using the following Regular Expression (PERL syntax):
    >
    "/^set\((?:'([^']+)'(?(?!\)),))+ \)$/"
    >
    The resulting matches array looks like this:
    >
    Array
    (
    [0] =Array
    (
    [0] =set('Hello','W orld')
    )
    >
    [1] =Array
    (
    [0] =World
    )
    >
    )
    >
    I wonder why "Hello" is not in the matches array. Did I do something
    wrong in my Regular Expression?
    >
    Thanks in advance.
    Ingmar
    >
    Hi Ingmar

    grouping is not automatic, to capture N strings you must have N groups
    in your expression or use preg_match_all, for example:

    $re1 = '/set\( (.*?) \)/x';
    $re2 = "/' (?: \\\\. | [^'] )* ' /x";

    preg_match($re1 , "set('Hello','W o\'rld')", $m);
    preg_match_all( $re2, $m[1], $m);
    print_r($m);




    --
    gosha bine

    extended php parser ~ http://code.google.com/p/pihipi
    blok ~ http://www.tagarga.com/blok

    Comment

    Working...