943,936 Members | Top Members by Rank

Ad:
  • Perl Discussion Thread
  • Marked Solved
  • Views: 1589
  • Perl RSS
Jul 31st, 2009
0

Backreference Variables

Expand Post »
I am reading the book "Beginning Perl" by James Lee and have been pretty happy with the content so far. That said I am stuck on chpt 7 which deals with regular expressions - I can't get the backreferences to work even when I copy the source verbatim.

source:

#!/usr/bin/perl -w
# matchtest2.pl
use strict;

$_ = '1: A silly sentence (495,a) *BUT* one which will be useful. (3)';

print "Enter a regular expression: ";
my $pattern = <STDIN>; chomp($pattern);

if (/$pattern/) {
print "The text matches the pattern '$pattern'.\n";
print "\$1 is '$1'\n" if defined $1;
print "\$2 is '$2'\n" if defined $2;
print "\$3 is '$3'\n" if defined $3;
print "\$4 is '$4'\n" if defined $4;
print "\$5 is '$5'\n" if defined $5;
} else {
print "'$pattern' was not found.\n";
}


Printing the $& variable displays 409 with expression \b\w{3}\b, but the $1 variable shows nothing.
I'm using perl, v5.10.0 built for i486-linux-gnu-thread-multi

Is this syntax outdated or obsolete?
Any and all help is appreciated.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
perlfan is offline Offline
7 posts
since Jul 2009
Jul 31st, 2009
0

Re: Backreference Variables

What are you entering into $pattern?
Reputation Points: 246
Solved Threads: 67
Practically a Posting Shark
KevinADC is offline Offline
898 posts
since Mar 2006
Jul 31st, 2009
0

Re: Backreference Variables

Click to Expand / Collapse  Quote originally posted by KevinADC ...
What are you entering into $pattern?
Multiple patterns:

jondev:/home/jondev/begperl/chp7> matchtest1.pl
Enter a regular expression: \b\w{3}\b
\b\w{3}\b found in 1: A silly sentence (495,a) *BUT* one which will be useful. (3)
$& = 495
jondev:/home/jondev/begperl/chp7>
>>>
jondev:/home/jondev/begperl/chp7> matchtest1.pl
Enter a regular expression: s\w+
s\w+ found in 1: A silly sentence (495,a) *BUT* one which will be useful. (3)
$& = silly
jondev:/home/jondev/begperl/chp7>
>>>
jondev:/home/jondev/begperl/chp7> matchtest1.pl
Enter a regular expression: [^0-9 ^A-Z]\w+
[^0-9 ^A-Z]\w+ found in 1: A silly sentence (495,a) *BUT* one which will be useful. (3)
$& = silly
jondev:/home/jondev/begperl/chp7>
>>>
jondev:/home/jondev/begperl/chp7> matchtest1.pl
Enter a regular expression: [^0-9 ^A-Z ^a-z]\w+
[^0-9 ^A-Z ^a-z]\w+ found in 1: A silly sentence (495,a) *BUT* one which will be useful. (3)
$& = (495
jondev:/home/jondev/begperl/chp7>
>>>

#!/usr/bin/perl -w
#matchtest1.pl
use strict;

my($pattern);
$_ = '1: A silly sentence (495,a) *BUT* one which will be useful. (3)';

print "Enter a regular expression: ";
chomp($pattern = <STDIN>);

if (/$pattern/){
print "$pattern found in $_\n";
print "\$& = $&\n";
print "\$1 is '$1'\n" if defined $1;
#print "\$2 is '$2'\n" if defined $2;
#print "\$3 is '$3'\n" if defined $3;
#print "\$4 is '$4'\n" if defined $4;
#print "\$5 is '$5'\n" if defined $5;
}
else{
print "$pattern NOT found\n";
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
perlfan is offline Offline
7 posts
since Jul 2009
Jul 31st, 2009
0

Re: Backreference Variables

You need parentheses for back references to capture patterns they find.


if (/($pattern)/) {
Reputation Points: 246
Solved Threads: 67
Practically a Posting Shark
KevinADC is offline Offline
898 posts
since Mar 2006
Jul 31st, 2009
0

Re: Backreference Variables

Click to Expand / Collapse  Quote originally posted by KevinADC ...
You need parentheses for back references to capture patterns they find.


if (/($pattern)/) {
Thanks KevinADC!
didn't see any reference in the book to this issue but now works like a champ!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
perlfan is offline Offline
7 posts
since Jul 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Perl Forum Timeline: Randomize Hash Iteration
Next Thread in Perl Forum Timeline: Memory Managment?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC