如何在规则中排除多个字符? [英] How to exclude more than one character in rule?

查看:57
本文介绍了如何在规则中排除多个字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在ANTLRWorks中编写一个字符串匹配规则,并且我需要匹配转义的引号或任何非引号字符.我可以匹配转义的引号,但其他部分遇到麻烦:~'\'' | ~'\"'最终将匹配所有内容,并且~'\'\"'似乎被语法生成器忽略(至少是视觉显示).什么字符序列可以让我得到我想要的东西?

I'm trying to write a string matching rule in ANTLRWorks, and I need to match either escaped quotes or any non quote character. I can match escaped quotes but I'm having trouble with the other part: ~'\'' | ~'\"' will end up matching everything and ~'\'\"' seems to be ignored by the grammar generator (at least the visual display). What sequence of characters will get me what I want?

推荐答案

尝试如下操作:

StringLiteral
    :    '"' (EscapeSequence | StringChar)* '"'
    ;

EscapeSequence
    :    '\\' ('"' | '\\')
    ;

StringChar
    :    ~('"' | '\\')
    ;

这篇关于如何在规则中排除多个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆