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

前端之家收集整理的这篇文章主要介绍了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.f2er.com/Perl/171853.html

猜你在找的Perl相关文章