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

Would you please help me by explaining this regular expression code. Thank you.
(/(is) \1/)

Replies are listed 'Best First'.
Re: Code Explaining
by nardo (Friar) on Aug 09, 2000 at 19:18 UTC
    The \1 is a backreference and will match the same thing that the first parenthesis matched. It isn't all that useful in this example since it could be rewritten as /(is) is/ but something like /(\d)\1/ would match on the same number twice in a row (because the \1 matches the same number that was found with the (\d)), thinking up more useful examples will be an exercise left up to the reader.
RE: Code Explaining
by jlistf (Monk) on Aug 09, 2000 at 19:23 UTC
    (simplified to some extent)
    the two /'s specify the matching operator (also seen as m//). this match operator allows you to search for certain regular expressions in a string. in this case, you are searching for 'is' which gets captured by the parentheses. this means that 'is' gets stored in the \1 variable. so you are searching for 'is' then a single space, then another 'is'. so where are you searching? well, the matching operator defaults to the $_ special variable (doesn't everything?). so you are looking in $_ for 'is is'. you can also specify a string to search in using the binding operator =~. so this expression could also be written as:
    $_ =~ m/is is/ # or $_ =~ m/(is) \1/
    the parentheses around the statement simply means that this is a test (for an if or a while or something).

    you should really check out the perlre manpage or 'Learning Perl' by O'Reilly or something for a much more detailed explanation of regular expressions.

    jeff
Re: Code Explaining
by Anonymous Monk on Aug 09, 2000 at 20:12 UTC
    Thank you very much for your response. Did you guys by any chance know if there is any good regular expression website.Thanks again.
      The perlre man page is quite useful. Mastering Regular Expressions is a great book (IMO), and is now available to read online (link removed by KM).

      Cheers,
      KM

        Ok, the following is a message that I sent to the author of the book, and his reply:
        |> I stumbled across your book online at 
        |> http://public.yahoo.com/~jfriedl/mre/
        
        Wow, could I ask how you stumbled across that? It's not supposed to
        be public.
        
        |> it appeared that you may not have intended to have the entire book 
        |> freely available online.
        |> 
        |> Am I free to post links to the full online version of your book?
        
        Please don't, and thanks for asking / bringing it to my attention.
        If you've grabbed a copy for yourself, please don't share it publically.
        It's probably too late (I see from the logs that a number of copies
        have gone out, so I'm probably sunk anyway )-:
        
        Again, thanks for bringing it to my attention,
                Jeffrey
        
        KM,
        [snip text accusing KM of posting pirated material]

        Update: Very sorry, I just looked at the URL and noticed that it contains the authors name. So apparently this is a perfectly legit link. Its great to see authors posting their work online, and I voted your post ++.