How do you comment a Perl regular expression?

2019-01-23 04:52发布

How do you put comments inside a Perl regular expression?

2条回答
forever°为你锁心
2楼-- · 2019-01-23 05:40

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.

查看更多
够拽才男人
3楼-- · 2019-01-23 05:58

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";
}
查看更多
登录 后发表回答