How do you comment a Perl regular expression?

柔情痞子 提交于 2019-11-27 21:16:18

问题


How do you put comments inside a Perl regular expression?


回答1:


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.




回答2:


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";
}


来源:https://stackoverflow.com/questions/632795/how-do-you-comment-a-perl-regular-expression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!