9

In ANTLR v4, how do we parse this kind of string with double quote escaped double quotes like in VBA?

for text:

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

the goal would be to identify the string: some string with "john doe" in it

And is it possible to rewrite it to turn double double quotes in single double quotes? "" -> "?

4

1 回答 1

15

像这样:

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);
   }
 ;
于 2013-07-27T12:47:56.227 回答