gpt4 book ai didi

regex - perl6如何在正则表达式内插中使用结点?

转载 作者:行者123 更新时间:2023-12-04 11:33:19 25 4
gpt4 key购买 nike

有时我的列表很长,我想检查字符串是否与列表中的任何内容匹配。我试图在正则表达式内插值。它们都是错误。

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.



使用列表,而不是连接点:
my @list = <bar bartoo baragain>;
say 'bartoo' ~~ / @list /; # 「bartoo」
say 'bartoo' ~~ / <{<bar bartoo baragain>}> /; # 「bartoo」

请注意,默认情况下,您会获得最长的匹配 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的模式不支持它们似乎是合理的。

The work-around I have is


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

如果您使用双倍的管道( ||)加入,则将获得第一个匹配的 token ,而不是最长的 token :
say 'bartoo' ~~ / <{'bar || bartoo || baragain'}> /; # 「bar」
say 'bartoo' ~~ / ||@list /; # 「bar」
say 'bartoo' ~~ / ||<{<bar bartoo baragain>}> /; # 「bar」

不为这些构造指定管道符号与指定单个管道符号( |)相同,并且匹配最长的匹配标记:
say 'bartoo' ~~ / <{'bar | bartoo | baragain'}> /; # 「bartoo」
say 'bartoo' ~~ / |@list /; # 「bartoo」
say 'bartoo' ~~ / |<{<bar bartoo baragain>}> /; # 「bartoo」

您之前曾问过相关问题。为了方便起见,我将在其中添加一些链接:
  • Is using junctions in matching possible?
  • Interpolate array in match for AND, OR, NOT functions
  • 关于regex - perl6如何在正则表达式内插中使用结点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53992260/

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