perl6如何在正则表达式内插中使用结点? [英] perl6 How to use junction inside regex interpolation?
问题描述
有时我的列表很长,我想检查字符串是否与列表中的任何内容匹配.我试图在正则表达式内插值.它们都是错误.
Sometimes I have a long list and I would like to check whether a string matches anything in the list. I am trying to interpolate a junction inside a regex. They are all errors.
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
此错误消息是否表示不能在正则表达式内插中使用结点?
Does this error message mean that junctions cannot be used inside regex interpolation?
我的解决方法是
say "12345" ~~ m/ <{ (2,3,4).join("||") }> /
「2」
关于在正则表达式内插中使用结点的任何建议吗?
Any suggestions as to use junctions inside regex interpolation?
非常感谢您!
lisprog
推荐答案
有时我的清单很长,我想检查字符串是否与清单中的任何内容匹配.
Sometimes I have a long list and I would like to check whether a string matches anything in the list.
使用列表,而不是连接点:
Use a list, not a Junction:
my @list = <bar bartoo baragain>;
say 'bartoo' ~~ / @list /; # 「bartoo」
say 'bartoo' ~~ / <{<bar bartoo baragain>}> /; # 「bartoo」
请注意,默认情况下,您会获得最长的匹配令牌.
Note that by default you get the longest matching token.
我正在尝试在正则表达式内插值.它们都是错误....此错误消息是否表示不能在正则表达式内插中使用结点?
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 的模式不支持它们,这似乎是合理的.
I think so. (The error message is perhaps LTA.) Junctions are a feature of the main P6 language. It seems reasonable that the pattern matching DSL doesn't support them.
我的解决方法是
say "12345" ~~ m/ <{ (2,3,4).join("||") }> /
「2」
如果您使用双倍的管道( ||
)加入,则将获得第一个匹配的令牌,而不是最长的令牌:
If you join with a doubled pipe (||
) then you get the first token that matches rather than the longest:
say 'bartoo' ~~ / <{'bar || bartoo || baragain'}> /; # 「bar」
say 'bartoo' ~~ / ||@list /; # 「bar」
say 'bartoo' ~~ / ||<{<bar bartoo baragain>}> /; # 「bar」
不为这些构造指定管道符号与指定单个管道符号( |
)相同,并且匹配最长匹配标记:
Not specifying the pipe symbol for these constructs is the same as specifying a single pipe symbol (|
) and matches the longest matching token:
say 'bartoo' ~~ / <{'bar | bartoo | baragain'}> /; # 「bartoo」
say 'bartoo' ~~ / |@list /; # 「bartoo」
say 'bartoo' ~~ / |<{<bar bartoo baragain>}> /; # 「bartoo」
您之前曾问过相关问题.为了方便起见,我将在其中添加一些链接:
You've asked related questions before. I'll add links to a couple of them here for convenience:
这篇关于perl6如何在正则表达式内插中使用结点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!