如何明确定义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
Run Code Online (Sandbox Code Playgroud)
当然我可以使用类似下面的内容,但我正在寻找一种惯用的方法.
> my regex null {a ** 0}
regex null {a ** 0}
> "abc" ~~ m/<null>abc/
?abc?
null => ??
Run Code Online (Sandbox Code Playgroud)
UPD:这有效,但这是最好的方法吗?
> my regex null { '' }
regex null { '' }
> "abc" ~~ m/<null>abc/
?abc?
null => ??
> "abc" ~~ m/('') abc/
?abc?
0 => ??
Run Code Online (Sandbox Code Playgroud)
始终成功并匹配0个字符的断言是:<?>.
例如:
say so 'foo' ~~ / <?> 'foo' /;
# output: True
Run Code Online (Sandbox Code Playgroud)
正如raiph指出的那样,相应的负面版本总是失败的<!>.如果输入有效,我已经使用它来在不应该达到部分解析时插入错误消息.正则表达式的那一部分可能是:
\d || [ { note "Error: the previous token must be followed by a number."; } <!> ]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
187 次 |
| 最近记录: |