perl6 – 在Perl 6中使正则表达式完全匹配零字符的最佳方法?

前端之家收集整理的这篇文章主要介绍了perl6 – 在Perl 6中使正则表达式完全匹配零字符的最佳方法?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何显式定义正则表达式(编号或命名的捕获组),它总是匹配零个字符?

> "abc" ~~ m/()abc/
===SORRY!=== Error while compiling:
Null regex not allowed

> my regex null {}
===SORRY!=== Error while compiling:
Null regex not allowed

> my regex null {<[]>}
regex null {<[]>}
> "abc" ~~ m/<null>abc/
False

当然我可以使用类似下面的内容,但我正在寻找一种惯用的方法.

> my regex null {a ** 0}
regex null {a ** 0}
> "abc" ~~ m/<null>abc/
「abc」
 null => 「」

UPD:这有效,但这是最好的方法吗?

> my regex null { '' }
regex null { '' }
> "abc" ~~ m/<null>abc/
「abc」
 null => 「」
> "abc" ~~ m/('') abc/
「abc」
 0 => 「」

解决方法

始终成功并匹配0个字符的断言是:<?>.

例如:

say so 'foo' ~~ / <?> 'foo' /;
# output: True

正如raiph所指出的那样,总是失败的相应负面版本是<!>.如果输入有效,我已经使用它来在不应该达到部分解析时插入错误消息.正则表达式的那一部分可能是:

\d || [ { note "Error: the prevIoUs token must be followed by a number."; } <!> ]

猜你在找的Perl相关文章