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

Can anyone explain me the code. What does "\1" mean in this code?

if (/(\b.+\b) \1/) { print "Found $1 repeated\n"; }

Thanks, Arun

Edit: g0n - added code tags

Replies are listed 'Best First'.
Re: What does this code mean(Reg Exp)?
by blazar (Canon) on Sep 27, 2005 at 10:21 UTC
    Do you know what $1 is? Well, \1 is just the same but within a regex. Well, now, why don't you give a peek into perlre?
      The clue was in the print statement. :-)

      and if you don't like pouring over online tutorials. "Mastering Regular Expressions" is a superb book.

      ---
      my name's not Keith, and I'm not reasonable.
Re: What does this code mean(Reg Exp)?
by Anonymous Monk on Sep 27, 2005 at 10:52 UTC
    Inside a regular expression, a construct of the form \NNN, that is, a backslash followed by one or more numbers, the first number not a zero, refers to the NNN'th set of parenthesis - that is, \1 matches exactly was matched by the first set of parenthesis.

    So, in your example, the code is looking for a sequence of characters, delimited by word/non-word boundaries, which repeats itself with a single space separating the sequences. Note that there isn't a requirement that the second sequence terminates (or starts) at a word boundary. "cat cats" matches. To match consecutive words, I would use: /(\b\w+\b)\s+\b\1\b/ - unlike the regex you presented that requires the sequence of characters to consist of word characters, allows more than one space between consecutive words, and requires the second sequence to be a complete word as well.