PerlMonkJunior has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I have one problem about the backreferences in regular expressions. I learned from the book "Beginning in Perl" (Page 162) that perl has a series of special variables in which it stores anything that is matched with a group in parentheses.Each time it sees a set of parentheses, it copies the matched text inside into a numbered variable- the first matched group goes in $1, the second group in $2 and so on. We call them as backreference variables.

However, when I tried out the following example in the book on my computer (the code is as follows):

#!/usr/bin/perl use warnings; use strict; $_ = 'This is a silly sentence. {3}. 2: but one may find it useful in +learning regular expression.'; print "please enter your pattern: " my $pattern = <STDIN>; chomp($pattern); if(/$pattern/) { print "I have found the pattern: "; print "\$1 is '$1'\n" if defined $1; } else { print "Sorry, we did not find anything this time.\n"; }
########## End of the Code ###################

When I enter a word "is" as the pattern, my program only said "I have found the pattern: ", but it did not print out the $1 it is supposed to print out. Can anyone help me out?

Thank you very much for your help in advance! Look forward to your reply soon.

Replies are listed 'Best First'.
Re: Please Help: A regular expression problem
by BrimBorium (Friar) on Sep 12, 2010 at 17:57 UTC
    if(/($pattern)/)

    will do. see perlre 'Capture buffers'. But I would prefer using a variable for string instead of setting $_ !

Re: Please Help: A regular expression problem
by ww (Archbishop) on Sep 13, 2010 at 00:59 UTC

    As BrimBorium said, you need a set of parens inside the match operation,

    if(/($pattern)/)

    as the outer pair are part of the conditional.

    And just BTW, you're missing a semi-colon after  print "please enter your pattern: " without which Perl will cough up a syntax error. When you post, it will serve you well to copy-paste code that compiles. In many cases, code that doesn't will simply cause some Monks (yeah, me included, sometimes) to throw up their hands and move on without trying to answer the underlying question (which, in any case, may be impossible to discern from a non-compiling code sample).

Re: Please Help: A regular expression problem
by JavaFan (Canon) on Sep 12, 2010 at 18:18 UTC
    Each time it sees a set of parentheses, it copies the matched text inside into a numbered variable

    ...

    When I enter a word "is" as the pattern

    How many sets of parentheses do you count in "is"?
Re: Please Help: A regular expression problem
by AnomalousMonk (Archbishop) on Sep 13, 2010 at 21:22 UTC

    Here's some code that lets you play with various patterns containing capture groups and also introduces the very useful @- and @+ arrays associated with the capture buffers. Try to explain the output from patterns like  (s(i(l+)y)) and in particular from  (is)|(si) versus  (si)|(is) regexes.

    >perl -wMstrict -le "my $str = 'This is a silly sentence. {3}.'; REGEX: { print 'please enter your pattern: '; my $pattern = <STDIN>; chomp $pattern; last REGEX unless length $pattern; if ($str =~ m{$pattern}) { print qq{found match for '$pattern'}; print 'no captures' and redo REGEX unless $#- > 0; for my $n (1 .. $#-) { my $capture_n = substr $str, $-[$n], $+[$n] - $-[$n]; print qq{\$$n is '$capture_n'}; } } else { print qq{NO match found for '$pattern'}; } redo REGEX; } "