Backreferences

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

    Backreferences

    Quote from PHP Manual:

    A back reference matches whatever actually matched the capturing
    subpattern in the current subject string, rather than
    anything matching the subpattern itself. So the pattern

    (sens|respons)e and \1ibility

    matches "sense and sensibility" and "response and responsibility" ,
    but not "sense and responsibility" .

    Is there a way to have a backreference matching anything matching the
    subpattern itself? So that one could get "sense and responsibility" as in
    the above example?

    I'm trying to find a way to express this:
    "/^(A)\?(B(?:\.B) *)\.(C)$/", where A, B and C are pattern like [^?.] (but
    more complex, especially B)
    but writing B just once, not twice. I thought backreferences were a good
    idea to solve this, but as they match only the actual "result" of the
    subpattern in question, this would not work here.

    Any solution?

    Thomas






  • P'tit Marcel

    #2
    Re: Backreferences

    Thomas Mlynarczyk écrivit:
    [color=blue]
    > (sens|respons)e and \1ibility
    >
    > matches "sense and sensibility" and "response and responsibility" ,
    > but not "sense and responsibility" .
    >
    > Is there a way to have a backreference matching anything matching the
    > subpattern itself? So that one could get "sense and responsibility" as in
    > the above example?[/color]


    You can just repeat the pattern :

    (sens|respons)e and (sens|respons)i bility



    --
    P'tit Marcel

    Comment

    • Thomas Mlynarczyk

      #3
      Re: Backreferences

      Also sprach P'tit Marcel:
      [color=blue][color=green]
      >> (sens|respons)e and \1ibility
      >>
      >> matches "sense and sensibility" and "response and
      >> responsibility" , but not "sense and responsibility" .
      >>
      >> Is there a way to have a backreference matching anything matching the
      >> subpattern itself? So that one could get "sense and responsibility"
      >> as in the above example?[/color]
      >
      >
      > You can just repeat the pattern :
      >
      > (sens|respons)e and (sens|respons)i bility[/color]

      Precisely what I want to avoid.

      --



      Comment

      Working...