Back references

Back references

am 14.11.2007 20:22:29 von Frieza

Hi I am studying Perl on my ciw course, I was wondering if anyone
could
tell me what back references are in regular expressions in plain
simple english, cause my course notes that I have, I need a dictionary
everytime I read a sentence.
Thanks

Re: Back references

am 14.11.2007 20:50:46 von Philip Potter

Frieza wrote:
> Hi I am studying Perl on my ciw course, I was wondering if anyone
> could
> tell me what back references are in regular expressions in plain
> simple english, cause my course notes that I have, I need a dictionary
> everytime I read a sentence.

Get a copy of "Programming Perl" or "Learning Perl", both by Larry Wall
et al. They're very good textbooks absolutely stuffed with plain simple
english.

Backreferences are the $1..$9 variables defined after a regular
expression has been applied to a string. They contain the substrings
which match the bits in parentheses. For example:

#!/usr/bin/perl
use strict;

my $string = "aaa bbb";
$string =~ /(a*) (b*)/;
print "\$1 = '$1' and \$2 = '$2'\n";


Phil

--
Philip Potter pgp doc.ic.ac.uk

Re: Back references

am 14.11.2007 20:56:49 von Jim Gibson

In article <1195068149.863306.167090@19g2000hsx.googlegroups.com>,
Frieza wrote:

> Hi I am studying Perl on my ciw course, I was wondering if anyone
> could
> tell me what back references are in regular expressions in plain
> simple english, cause my course notes that I have, I need a dictionary
> everytime I read a sentence.
> Thanks

A back reference in a regular expression is a value that has been
matched and captured earlier ("back") in the pattern. Perl back
references are denoted by \1, \2, etc.

For example, if you wish to match any two consecutive and identical
lower-case letters, you can use the regular expression:

m/([a-z])\1/

Note how this will differ from the regular expression

m/[a-z][a-z]/

which will match any two consecutive lower-case letters not necessarily
the same.

See also

perldoc perlretut
perldoc perlre

and search for 'backreference'.

--
Jim Gibson

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com

Re: Back references

am 14.11.2007 20:58:00 von Frieza

On 14 Nov, 19:50, Philip Potter wrote:
> Frieza wrote:
> > Hi I am studying Perl on my ciw course, I was wondering if anyone
> > could
> > tell me what back references are in regular expressions in plain
> > simple english, cause my course notes that I have, I need a dictionary
> > everytime I read a sentence.
>
> Get a copy of "Programming Perl" or "Learning Perl", both by Larry Wall
> et al. They're very good textbooks absolutely stuffed with plain simple
> english.
>
> Backreferences are the $1..$9 variables defined after a regular
> expression has been applied to a string. They contain the substrings
> which match the bits in parentheses. For example:
>
> #!/usr/bin/perl
> use strict;
>
> my $string = "aaa bbb";
> $string =~ /(a*) (b*)/;
> print "\$1 = '$1' and \$2 = '$2'\n";
>
> Phil
>
> --
> Philip Potter pgp doc.ic.ac.uk

Thanks Phil that makes it alot clearer already, I will definetly look
up those books by Larry Wall.
Thanks

Re: Back references

am 14.11.2007 21:09:29 von Frieza

On 14 Nov, 19:56, Jim Gibson wrote:
> In article <1195068149.863306.167...@19g2000hsx.googlegroups.com>,
>
> Frieza wrote:
> > Hi I am studying Perl on my ciw course, I was wondering if anyone
> > could
> > tell me what back references are in regular expressions in plain
> > simple english, cause my course notes that I have, I need a dictionary
> > everytime I read a sentence.
> > Thanks
>
> A back reference in a regular expression is a value that has been
> matched and captured earlier ("back") in the pattern. Perl back
> references are denoted by \1, \2, etc.
>
> For example, if you wish to match any two consecutive and identical
> lower-case letters, you can use the regular expression:
>
> m/([a-z])\1/
>
> Note how this will differ from the regular expression
>
> m/[a-z][a-z]/
>
> which will match any two consecutive lower-case letters not necessarily
> the same.
>
> See also
>
> perldoc perlretut
> perldoc perlre
>
> and search for 'backreference'.
>
> --
> Jim Gibson
>
> Posted Via Usenet.com Premium Usenet Newsgroup Services
> ----------------------------------------------------------
> ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
> ----------------------------------------------------------
> http://www.usenet.com

Jim thanks very much the added detail has helped alot.
Thanks again

Re: Back references

am 14.11.2007 22:09:57 von Uri Guttman

>>>>> "PP" == Philip Potter writes:

PP> Frieza wrote:
>> Hi I am studying Perl on my ciw course, I was wondering if anyone
>> could
>> tell me what back references are in regular expressions in plain
>> simple english, cause my course notes that I have, I need a dictionary
>> everytime I read a sentence.

PP> Get a copy of "Programming Perl" or "Learning Perl", both by Larry Wall
PP> et al. They're very good textbooks absolutely stuffed with plain simple
PP> english.

PP> Backreferences are the $1..$9 variables defined after a regular
PP> expression has been applied to a string. They contain the substrings
PP> which match the bits in parentheses. For example:

that is incorrect. those are grabs and the scalar variables used to
access them later on (in the replacement string or later).

backreferences are when you refer to a previous grab INSIDE the same
regex. $1 inside a regex will refer to a grab from an earler regex grab,
not the current one. you use \1 to refer to the first grab in the
current regex.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org

Re: Back references

am 14.11.2007 22:10:25 von Uri Guttman

>>>>> "F" == Frieza writes:

F> Thanks Phil that makes it alot clearer already, I will definetly look
F> up those books by Larry Wall.

and he was wrong. see my other post in this thread.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org

Re: Back references

am 15.11.2007 01:00:18 von Tad McClellan

Philip Potter wrote:
> Frieza wrote:
>> Hi I am studying Perl on my ciw course, I was wondering if anyone
>> could
>> tell me what back references are in regular expressions in plain
>> simple english, cause my course notes that I have, I need a dictionary
>> everytime I read a sentence.
>
> Get a copy of "Programming Perl" or "Learning Perl", both by Larry Wall


Only one of those was by Larry, the other one was by Randal et. al.


> They're very good textbooks absolutely stuffed with plain simple
> english.


That part, at least, is accurate.


> #!/usr/bin/perl
> use strict;


You should always enable warnings when developing Perl code.


> $string =~ /(a*) (b*)/;
> print "\$1 = '$1' and \$2 = '$2'\n";


You should never use the dollar-digit variables unless you have
first ensured that the match *succeeded*.

if ( $string =~ /(a*) (b*)/ ) {
print "\$1 = '$1' and \$2 = '$2'\n";
}


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

Re: Back references

am 15.11.2007 14:13:35 von Philip Potter

Uri Guttman wrote:
> that is incorrect. those are grabs and the scalar variables used to
> access them later on (in the replacement string or later).
>
> backreferences are when you refer to a previous grab INSIDE the same
> regex. $1 inside a regex will refer to a grab from an earler regex grab,
> not the current one. you use \1 to refer to the first grab in the
> current regex.

Whoops! Thanks for the correction. (And thanks to Tad as well.)

--
Philip Potter pgp doc.ic.ac.uk