perl6是否可以在匹配中使用结点? [英] perl6 Is using junctions in matching possible?

查看:74
本文介绍了perl6是否可以在匹配中使用结点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用结点匹配结点中的任何值?我想匹配数组中的任何值.正确的方法是什么?

Is it possible to use junction to match any of the values in a junction? I want to match any of the values in an array. What is the proper way to do it?

lisprog$ perl6
To exit type 'exit' or '^D'
> my @a=<a b c>
[a b c]
> any(@a)
any(a, b, c)
> my $x=any(@a)
any(a, b, c)
> my $y = "a 1"
a 1
> say $y ~~ m/ $x /
False
> say $y ~~ m/ "$x" /
False
> my $x = any(@a).Str
any("a", "b", "c")
> say $y ~~ m/ $x /
False
> say $y ~~ m/ || $x /
False
> say $y ~~ m/ || @a /
「a」
> 

谢谢!!

推荐答案

结点不能插入到正则表达式中.它们应在正常的Perl 6表达式中使用,尤其是在比较运算符(例如eq)中:

Junctions are not meant to be interpolated into regexes. They're meant to be used in normal Perl 6 expressions, particularly with comparison operators (such as eq):

my @a = <x y z>;
say    "y" eq any(@a);  # any(False, True, False)
say so "y" eq any(@a);  # True

要匹配正则表达式中数组的任何值,只需在正则表达式中写入数组变量的名称(以@开头).默认情况下,这被解释为|替代(最长匹配"),但是您也可以将其指定为||替代(第一匹配"):

To match any of the values of an array in a regex, simply write the name of the array variable (starting with @) in the regex. By default, this is interpreted as an | alternation ("longest match"), but you can also specify it to be a || alternation ("first match"):

my @a = <foo bar barkeep>;
say "barkeeper" ~~ / @a /;     # 「barkeep」
say "barkeeper" ~~ / || @a /;  # 「bar」

这篇关于perl6是否可以在匹配中使用结点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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