如何在规则中排除多个字符?

问题描述:

我正在尝试在 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
    :    ~('"' | '\\')
    ;