back references

Discussion in 'Perl Misc' started by REH, Feb 8, 2005.

  1. REH

    REH Guest

    How to a specify a back reference immediately followed by a number without
    Perl thinking its part of the back reference?
    REH, Feb 8, 2005
    #1
    1. Advertising

  2. REH

    Anno Siegel Guest

    REH <> wrote in comp.lang.perl.misc:
    > How to a specify a back reference immediately followed by a number without
    > Perl thinking its part of the back reference?


    One way:

    $_ = 'aba2';
    /(.).\1(?:2)/ and print "ok\n";

    There are others...

    Anno
    Anno Siegel, Feb 8, 2005
    #2
    1. Advertising

  3. REH

    REH Guest

    "Jim Gibson" <> wrote in message
    news:080220051432021454%...
    > In article <cubce4$>, REH <>
    > wrote:
    >
    > > How to a specify a back reference immediately followed by a number

    without
    > > Perl thinking its part of the back reference?

    >
    > I am guessing you mean backreferences within a regular expression, such
    > as the \1 in m/(.)\1/ to match any doubled character. (It would have
    > been nice to have posted a program demonstrating the problem.)
    >
    > Surround the backreference in parentheses, either capturing or
    > non-capturing:
    >
    > m/(.)(\1)2/
    > or
    > m/(.)(?:\1)2/
    >
    > to match any doubled-character followed by a 2.
    >
    >

    I'm sorry for being vague. I mean using backreferences in the replacement
    part of a regular expression. I need to have a "\1" immediately followed by
    a number, such as "\11" but the last one is not part of the backreference.

    Thanks
    REH, Feb 9, 2005
    #3
  4. REH

    Anno Siegel Guest

    REH <> wrote in comp.lang.perl.misc:
    > "Jim Gibson" <> wrote in message
    > news:080220051432021454%...
    > > In article <cubce4$>, REH <>
    > > wrote:
    > >
    > > > How to a specify a back reference immediately followed by a number

    > without
    > > > Perl thinking its part of the back reference?


    [proposed solution]

    > I'm sorry for being vague. I mean using backreferences in the replacement
    > part of a regular expression. I need to have a "\1" immediately followed by
    > a number, such as "\11" but the last one is not part of the backreference.


    You don't use that kind of backreference in the replacement part, you
    use the capturing variables $1, $2, etc. This is simple string
    interpolation: "${1}2".

    Anno
    Anno Siegel, Feb 9, 2005
    #4
  5. REH

    REH Guest

    "Anno Siegel" <-berlin.de> wrote in message
    news:cudbuc$n1g$-Berlin.DE...
    > > I'm sorry for being vague. I mean using backreferences in the

    replacement
    > > part of a regular expression. I need to have a "\1" immediately

    followed by
    > > a number, such as "\11" but the last one is not part of the

    backreference.
    >
    > You don't use that kind of backreference in the replacement part, you
    > use the capturing variables $1, $2, etc. This is simple string
    > interpolation: "${1}2".
    >
    > Anno


    Thank you. That's interesting. I've always used the backslash form and
    Perl allowed it. Is there a difference between the two?
    REH, Feb 9, 2005
    #5
  6. REH

    Anno Siegel Guest

    REH <> wrote in comp.lang.perl.misc:
    >
    > "Anno Siegel" <-berlin.de> wrote in message
    > news:cudbuc$n1g$-Berlin.DE...
    > > > I'm sorry for being vague. I mean using backreferences in the

    > replacement
    > > > part of a regular expression. I need to have a "\1" immediately

    > followed by
    > > > a number, such as "\11" but the last one is not part of the

    > backreference.
    > >
    > > You don't use that kind of backreference in the replacement part, you
    > > use the capturing variables $1, $2, etc. This is simple string
    > > interpolation: "${1}2".
    > >
    > > Anno

    >
    > Thank you. That's interesting. I've always used the backslash form and
    > Perl allowed it. Is there a difference between the two?


    Use \1, etc only inside the regex itself (you don't need that often).
    Use $1, etc everywhere else, including the replacement part of s///.
    See "perldoc perlre", look for "Warning on" (... \1 vs $1).

    Anno.


    Anno
    Anno Siegel, Feb 9, 2005
    #6
  7. REH

    Anno Siegel Guest

    REH <> wrote in comp.lang.perl.misc:
    >
    > "Anno Siegel" <-berlin.de> wrote in message
    > news:cudbuc$n1g$-Berlin.DE...
    > > > I'm sorry for being vague. I mean using backreferences in the

    > replacement
    > > > part of a regular expression. I need to have a "\1" immediately

    > followed by
    > > > a number, such as "\11" but the last one is not part of the

    > backreference.
    > >
    > > You don't use that kind of backreference in the replacement part, you
    > > use the capturing variables $1, $2, etc. This is simple string
    > > interpolation: "${1}2".
    > >
    > > Anno

    >
    > Thank you. That's interesting. I've always used the backslash form and
    > Perl allowed it. Is there a difference between the two?


    Use \1, etc only inside the regex itself (you don't need that often).
    Use $1, etc everywhere else, including the replacement part of s///.
    See "perldoc perlre", look for "Warning on" (... \1 vs $1).

    Anno
    Anno Siegel, Feb 9, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roger Leigh
    Replies:
    8
    Views:
    422
    Karl Heinz Buchegger
    Nov 17, 2003
  2. Replies:
    3
    Views:
    439
    Victor Bazarov
    Nov 10, 2004
  3. DanielEKFA
    Replies:
    8
    Views:
    592
    DanielEKFA
    May 16, 2005
  4. Replies:
    8
    Views:
    698
    Bruno Desthuilliers
    Dec 12, 2006
  5. Lars Willich
    Replies:
    13
    Views:
    823
    Ian Shef
    Oct 23, 2007
Loading...

Share This Page