ANTLRv4:如何读取字符串中的双引号转义双引号? [英] ANTLRv4: How to read double quote escaped double quotes in string?

查看:559
本文介绍了ANTLRv4:如何读取字符串中的双引号转义双引号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

对于文本:

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

目标是识别字符串: 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? "" -> "?

推荐答案

像这样:

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

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

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

是否可以重写它以将双双引号转换为单双引号?

并非没有嵌入自定义代码.在Java中看起来像这样:

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);
   }
 ;

这篇关于ANTLRv4:如何读取字符串中的双引号转义双引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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