加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 21:55:23 所属栏目:大数据 来源:网络整理
导读:我是perl的新手,并试图设计一个我遇到的词法分析器: my @token_def = ( [Whitespace = qr{s+},1],[Comment = qr{#.*n?$}m,); 甚至在经过多个网站后我都不理解其含义. 解决方法 qr //是适用于模式匹配和相关活动的类似引号的运算符之一. 从perldoc开始: T
我是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!' );

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读