如何在 perl6 正则表达式插值中使用联结?

How to use junction inside a perl6 regex interpolation?

有时我有一个很长的列表,我想检查一个字符串是否与列表中的任何内容匹配。我正在尝试在正则表达式中插入一个连接点。都是错误。

say "12345" ~~ m/ <{ (2,3,4).any }> /
Cannot resolve caller MAKE_REGEX(Int, Bool, Bool, Int, PseudoStash); none of these signatures match:

say "12345" ~~ m/ $( (2,3,4).any ) /
This type cannot unbox to a native string: P6opaque, Junction

此错误消息是否意味着不能在正则表达式插值中使用联结?

我的解决方法是

say "12345" ~~ m/ <{ (2,3,4).join("||") }> /
「2」

如何在正则表达式插值中使用联结?

Sometimes I have a long list and I would like to check whether a string matches anything in the list.

使用列表,而不是 Junction:

my @list = <bar bartoo baragain>;
say 'bartoo' ~~ / @list /;                         # 「bartoo」
say 'bartoo' ~~ / <{<bar bartoo baragain>}> /;     # 「bartoo」

请注意,默认情况下您会获得最长的匹配标记。

I am trying to interpolate a junction inside a regex. They are all errors. ... Does this error message mean that junctions cannot be used inside regex interpolation?

我也这么认为。 (错误消息可能是 LTA。)连接是主要 P6 语言的一个特性。模式匹配 DSL 不支持它们似乎是合理的。

The work-around I have is

say "12345" ~~ m/ <{ (2,3,4).join("||") }> /
「2」

如果您使用双管道 (||) 加入,那么您会得到第一个匹配的标记而不是最长的:

say 'bartoo' ~~ / <{'bar || bartoo || baragain'}> /; # 「bar」
say 'bartoo' ~~ / ||@list /;                         # 「bar」
say 'bartoo' ~~ / ||<{<bar bartoo baragain>}> /;     # 「bar」

Not 为这些构造指定管道符号与指定单个管道符号 (|) 相同并匹配最长的匹配标记:

say 'bartoo' ~~ / <{'bar | bartoo | baragain'}> /; # 「bartoo」
say 'bartoo' ~~ / |@list /;                        # 「bartoo」
say 'bartoo' ~~ / |<{<bar bartoo baragain>}> /;    # 「bartoo」

你以前问过相关问题。为了方便起见,我将在此处添加其中几个的链接: