Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you comment a Perl regular expression?

How do you put comments inside a Perl regular expression?

like image 633
Eric Johnson Avatar asked Mar 11 '09 00:03

Eric Johnson


People also ask

How do you comment in regex?

A number sign ( # )marks an x-mode comment, which starts at the unescaped # character at the end of the regular expression pattern and continues until the end of the line.

What is \W in Perl regex?

A \w matches a single alphanumeric character (an alphabetic character, or a decimal digit) or _ , not a whole word. Use \w+ to match a string of Perl-identifier characters (which isn't the same as matching an English word).

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string).

What does \s mean in Perl?

In addition, Perl defines the following: \w Match a "word" character (alphanumeric plus "_") \W Match a non-word character \s Match a whitespace character \S Match a non-whitespace character \d Match a digit character \D Match a non-digit character.


2 Answers

Use the /x modifier:

my $foo = "zombies are the bombies";
if ($foo =~ /
             zombie  # sorry pirates
            /x ) {
    print "urg. brains.\n";
}

Also see the first question in perlfaq6.

Also it wouldn't hurt to read all of perlre while you're at it.

like image 148
Eric Johnson Avatar answered Sep 23 '22 21:09

Eric Johnson


Even without the /x modifier, you can enclose comments in (?# ... ):

my $foo = "zombies are the bombies";
if ( $foo =~ /zombie(?# sorry pirates)/ ) {
    print "urg. brains.\n";
}
like image 37
ysth Avatar answered Sep 21 '22 21:09

ysth