perl中qr//是什么意思 [英] What is the meaning of qr// in perl

查看:107
本文介绍了perl中qr//是什么意思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 perl 完全陌生,并试图设计一个我遇到的词法分析器:

I am completely new to perl and trying to design a lexer where I have come across:

my @token_def =
 (
        [Whitespace => qr{\s+},     1],
        [Comment    => qr{#.*\n?$}m,   1],
  );

即使在浏览了多个网站后,我也不明白其中的含义.

and even after going through multiple sites I did not understand the meaning.

推荐答案

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

qr// is one of the quote-like operators that apply to pattern matching and related activities.

来自 perldoc:

此运算符引用(并可能编译)其 STRING 作为正则表达式.STRING 的插值方式与 m/PATTERN/中的 PATTERN 相同.如果使用 ' 作为分隔符,则不进行插值.

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:

qr//运算符创建一流的正则表达式.将它们插入到匹配运算符中以使用它们:

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/;

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

... or combine multiple regex objects into complex patterns:

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!' );

这篇关于perl中qr//是什么意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆