加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 23:21:14 所属栏目:大数据 来源:网络整理
导读:如何显式定义正则表达式(编号或命名的捕获组),它总是匹配零个字符? "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 nu
如何显式定义正则表达式(编号或命名的捕获组),它总是匹配零个字符?

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读