Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > back references

Reply
Thread Tools

back references

 
 
REH
Guest
Posts: n/a
 
      02-08-2005
How to a specify a back reference immediately followed by a number without
Perl thinking its part of the back reference?


 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      02-08-2005
REH <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
REH
Guest
Posts: n/a
 
      02-09-2005

"Jim Gibson" <(E-Mail Removed)> wrote in message
news:080220051432021454%(E-Mail Removed). ..
> In article <cubce4$(E-Mail Removed)>, REH <(E-Mail Removed)>
> 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


 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      02-09-2005
REH <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> "Jim Gibson" <(E-Mail Removed)> wrote in message
> news:080220051432021454%(E-Mail Removed). ..
> > In article <cubce4$(E-Mail Removed)>, REH <(E-Mail Removed)>
> > 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
 
Reply With Quote
 
REH
Guest
Posts: n/a
 
      02-09-2005

"Anno Siegel" <(E-Mail Removed)-berlin.de> wrote in message
news:cudbuc$n1g$(E-Mail Removed)-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?



 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      02-09-2005
REH <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>
> "Anno Siegel" <(E-Mail Removed)-berlin.de> wrote in message
> news:cudbuc$n1g$(E-Mail Removed)-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
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      02-09-2005
REH <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>
> "Anno Siegel" <(E-Mail Removed)-berlin.de> wrote in message
> news:cudbuc$n1g$(E-Mail Removed)-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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Snake references just as ok as Monty Python jokes/references in python community? :) seberino@spawar.navy.mil Python 8 12-12-2006 11:21 PM
Typedef A references struct B which references struct A which... DanielEKFA C++ 8 05-16-2005 10:26 AM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM
how to understand references to variables and references to constants are distinguished? baumann.Pan@gmail.com C++ 3 11-10-2004 04:16 AM
Pointers and References (and References to Pointers) Roger Leigh C++ 8 11-17-2003 10:14 AM



Advertisments