gpt4 book ai didi

regex - perl中的qr//是什么意思

转载 作者:行者123 更新时间:2023-12-02 09:39:26 25 4
gpt4 key购买 nike

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

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

即使浏览了多个网站我也不明白其含义。

最佳答案

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

关于regex - perl中的qr//是什么意思,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30093272/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com