ANTLR:匹配未转义的字符?
我有一个类似的规则,
charGroup
: '[' .+ ']';
但我猜它会匹配 [abc\]
之类的东西。假设我希望它仅匹配未转义的 ]
,我该怎么做?在正则表达式中,我会使用负向后查找。
编辑:如果可能的话,我也希望它不贪婪/懒惰。从而只匹配[a][b]
中的[a]
。
I've got a rule like,
charGroup
: '[' .+ ']';
But I'm guessing that'll match something like [abc\]
. Assuming I want it to match only unescaped ]
s, how do I do that? In a regular expression I'd use a negative look-behind.
Edit: I'd also like it to be ungreedy/lazy if possible. So as to match only [a]
in [a][b]
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能想做类似的事情:
其中
~('\\' | ']')
匹配除\
和]
之外的单个字符。请注意,您只能否定单个字符!不存在~('ab')
这样的东西。另一个经常犯的错误是否定内部解析器规则不会否定字符,而是否定标记。一个示例可能如下:现在解析器规则
foo
匹配标记B
或标记C
(因此只有字符'b'
和'c'
),而词法分析器规则D
匹配除'a'
之外的任何字符。You probably wanted to do something like:
where
~('\\' | ']')
matches a single character other than\
and]
. Note that you can only negate single characters! There's no such thing as~('ab')
. Another mistake often made is that negating inside parser rules does not negate a character, but a token instead. An example might be in order:Now parser rule
foo
matches either tokenB
or tokenC
(so only the characters'b'
and'c'
) while lexer ruleD
matches any character other than'a'
.这不是不必要的复杂吗?怎么样:
Isn't that unnecessarily complex? How about: