Problem with preg_match with capturing and back-reference

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

    #1

    Problem with preg_match with capturing and back-reference

    I'm trying to detect and extract a quoted portion of a string. The
    quote chars may be the usual quote, the "single" quote (apostrophe) or
    the back-tick quote. For example:
    $vl = "My O'Name";

    if (preg_match ( '/(["\'\`])([^\\1]*)/' ) , $vl , $x ) { // found it }

    The first parenthesized expression will capture the leading quote
    (whatever it is). The second *SHOULD* capture everything up to, but
    not including, the matching trailing quote. What I get is $x[1]
    contains the quote (as expected), but $x[2] is everything up to AND
    INCLUDING the trailing quote.

    This is PHP 5.1.2 with PCRE 6.2 01-Aug-2005

  • Chung Leong

    #2
    Re: Problem with preg_match with capturing and back-reference

    pt wrote:
    I'm trying to detect and extract a quoted portion of a string. The
    quote chars may be the usual quote, the "single" quote (apostrophe) or
    the back-tick quote. For example:
    $vl = "My O'Name";
    >
    if (preg_match ( '/(["\'\`])([^\\1]*)/' ) , $vl , $x ) { // found it }
    >
    The first parenthesized expression will capture the leading quote
    (whatever it is). The second *SHOULD* capture everything up to, but
    not including, the matching trailing quote. What I get is $x[1]
    contains the quote (as expected), but $x[2] is everything up to AND
    INCLUDING the trailing quote.
    >
    This is PHP 5.1.2 with PCRE 6.2 01-Aug-2005
    I don't think you can use a back reference in a character class. Try
    this pattern instead: /(["'`])(.*?)\1/.

    Comment

    Working...