Problem with preg_match expression

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jørn Dahl-Stamnes

    Problem with preg_match expression

    Hello,

    I'm trying to write an expression that will match lines like this:

    This is [a] a line[/a] with text
    This [w] is also a[/w] a line

    but not lines like:

    This [a] shall not[/w] match

    My expression looks like this:

    preg_match("/(.*)\[(.)\](.+)\[\/\2\](.*)/",$line,$matche s)

    But it does not match. I have tried to replace \2 part of the string above
    with a . and then it match.

    If I'm correct, the \2 should referre to the character matched by the .
    inside the []... or?

    Thanx in advace.

    --
    Jørn Dahl-Stamnes

  • John Dunlop

    #2
    Re: Problem with preg_match expression

    Jørn Dahl-Stamnes wrote:
    [color=blue]
    > "/(.*)\[(.)\](.+)\[\/\2\](.*)/"[/color]

    a) Escape the backreference; e.g.,
    /(.*)\[(.)\](.+)\[\/\\2\](.*)/
    b) Single-quote the regular expression.



    --
    Jock

    Comment

    • Jørn Dahl-Stamnes

      #3
      Re: Problem with preg_match expression

      John Dunlop wrote:
      [color=blue]
      > Jørn Dahl-Stamnes wrote:
      >[color=green]
      >> "/(.*)\[(.)\](.+)\[\/\2\](.*)/"[/color]
      >
      > a) Escape the backreference; e.g.,
      > /(.*)\[(.)\](.+)\[\/\\2\](.*)/
      > b) Single-quote the regular expression.[/color]

      Thanks. I found the answer just before I red your reply here.

      --
      Jørn Dahl-Stamnes

      Comment

      Working...