在 Perl 6 中制作正则表达式完全匹配零个字符的最佳方法?

The best way to make a regex matching exactly zero characters in Perl 6?

如何显式定义一个 regex(编号或命名的捕获组),它总是完全匹配零个字符?

> "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."; } <!> ]