微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

regex – 在perl中qr //的含义是什么?

我是perl的新手,并试图设计一个我遇到的词法分析器:
my @token_def =
 (
        [Whitespace => qr{\s+},1],[Comment    => qr{#.*\n?$}m,);

甚至在经过多个网站后我都不理解其含义.

解决方法

qr //是适用于模式匹配和相关活动的类似引号的运算符之一.

perldoc开始:

This operator quotes (and possibly compiles) its STRING as a regular expression. STRING is interpolated the same way as PATTERN in m/PATTERN/. If ' is used as the delimiter,no interpolation is done.

modern_perl开始:

The qr// operator creates first-class regexes. Interpolate them into the match operator to use them:

my $hat = qr/hat/;
say 'Found a hat!' if $name =~ /$hat/;

…或将多个正则表达式对象组合成复杂的模式:

my $hat   = qr/hat/;
my $field = qr/field/;

say 'Found a hat in a field!'
if $name =~ /$hat$field/;

like( $name,qr/$hat$field/,'Found a hat in a field!' );

原文地址:https://www.jb51.cc/Perl/171853.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐