How do you put comments inside a Perl regular expression?
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.
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).
$ means "Match the end of the string" (the position after the last character in the string).
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.
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.
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";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With