Back references

F

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
 
P

Philip Potter

Frieza said:
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
 
F

Frieza

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

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

Frieza

Frieza said:
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'.
 
U

Uri Guttman

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
 
U

Uri Guttman

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
 
T

Tad McClellan

Philip Potter said:
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";
}
 
P

Philip Potter

Uri said:
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.)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top