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

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

问题描述

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

对于文本:

"一些带有""john doe"" 的字符串"

目标是识别字符串:一些带有john doe"的字符串

而且有没有可能改写成双双引号变成单双引号?<代码>"" ->"?

解决方案

像这样:

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

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

~[\r\n"] # '\r'、'\n' 和双引号以外的任何字符|# 或者'""' # 两个连续的双引号

<块引用>

而且有没有可能改写成双双引号变成单双引号?

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

STRING: '"' (~[\r\n"] | '""')* '"'{字符串 s = getText();s = s.substring(1, s.length() - 1);//去除前导和尾随引号s = s.replace("\"\"", "\"");//用单引号替换所有双引号setText(s);};

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

解决方案

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

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

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