vinoth.ree has asked for the wisdom of the Perl Monks concerning the following question:

How do you put comments inside a Perl regular expression?

Replies are listed 'Best First'.
Re: comment inside perl regular expression
by moritz (Cardinal) on Mar 13, 2009 at 12:50 UTC
    With the /x modifier:
    my $re = qr{ "[^"]*" # first quoted string = URL ... # more stuff }x
      Or by doing something really wacky:
      /(?:(*FAIL)\QThis is a comment\E)?PATTERN/
      Not something I recommend doing though.
        If you want inline comments, you can use (?# comment ) instead, which is less wacky, but I still think that it's no great thing in terms of readability.
Re: comment inside perl regular expression
by toolic (Bishop) on Mar 13, 2009 at 13:13 UTC
Re: comment inside perl regular expression
by VinsWorldcom (Prior) on Mar 13, 2009 at 14:07 UTC

    Regex are tough enough to read without inserting comments into them. What's wrong with putting a detailed comment block above the regex explaining what/how it does what it does? That's what I do so when I inevitably go back to modify it at a later date, I don't have to 'learn' regex all over again.

      Compare these 3 I think the 1st one is the easiest
A reply falls below the community's threshold of quality. You may see it by logging in.