ANTLRv4: How to read double quote escaped double q

2019-02-09 10:00发布

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? "" -> "?

标签: antlr4
1条回答
再贱就再见
2楼-- · 2019-02-09 10:22

Like this:

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

where ~[\r\n"] | '""' means:

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

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

Not without embedding custom code. In Java that could look like:

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);
   }
 ;
查看更多
登录 后发表回答