ANTLR v4:如何在字符串中读取双引号转义双引号?

时间:2013-07-27 12:09:25

标签: antlr4

在ANTLR v4中,我们如何使用双引号转义双引号来解析这种字符串,如VBA?

表示文字:

"some string with ""john doe"" in it"

目标是识别字符串: some string with "john doe" in it

是否可以将其重写为单双引号中的双倍双引号? "" -> "

1 个答案:

答案 0 :(得分:11)

像这样:

STRING
 : '"' (~[\r\n"] | '""')* '"'
 ;

~[\r\n"] | '""'表示:

~[\r\n"]    # any char other than '\r', '\n' and double quotes
|           # OR
'""'        # two successive double quotes
  

是否有可能将其重写为单双引号中的双引号?

不嵌入自定义代码。在Java中看起来像:

STRING
 : '"' (~[\r\n"] | '""')* '"' 
   {
     String s = getText();
     s = s.substring(1, s.length() - 1); // strip the leading and trailing quotes
     s = s.replace("\"\"", "\""); // replace all double quotes with single quotes
     setText(s);
   }
 ;