【发布时间】:2013-06-02 03:24:05
【问题描述】:
考虑一个字符串,例如响应标头:HTTP/1.1 404 Not Found。
我很好奇您是否可以使用 smartmatch (~~ or double tilde) operator 和正则表达式的组合来搜索不完整的匹配项或匹配项的子集。
my $head = q{HTTP/1.1 404 Not Found};
my @success = (200, 201);
my @failure = (404, 409);
# Array First
say q{Success} if @success ~~ $head;
say q{Fail} if @failure ~~ $head;
# Array Second
say q{Success} if $head ~~ @success ;
say q{Fail} if $head ~~ @failure ;
我知道这可以通过某种循环、grep 或映射来完成;但我正在调查~~ 能做什么和不能做什么的可能性。
文档非常具有描述性,并且对于大多数 lhs/rhs 条件似乎遵循 对于 [all in left side] == [all in right side] 评估的返回 true。 p>
也就是说,如果$head 只是它会匹配的数字(例如$head=q{200})。
预期结果类似于:
my $head = q{HTTP/1.1 201 OK};
my @success = qw(200 201);
say q{Success} if grep{$head =~ /$_/ } @success;
【问题讨论】:
-
不幸的是,智能匹配运算符一直是re-declared experimental in 5.18.0,现在抛出警告。鉴于它持续不稳定,而且它的行为可能会改变,我建议不要使用它。
标签: regex perl list smartmatch