gpt4 book ai didi

antlr4 - ANTLRv4 : How to read double quote escaped double quotes in string?

转载 作者:行者123 更新时间:2023-12-04 06:20:12 28 4
gpt4 key购买 nike

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

对于文本:

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

目标是识别字符串: some string with "john doe" in it
可以重写它以将双双引号转换为单双引号吗? "" -> "

最佳答案

像这样:

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

其中 ~[\r\n"] | '""'的意思是:
~[\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?



并非没有嵌入自定义代码。在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);
}
;

关于antlr4 - ANTLRv4 : How to read double quote escaped double quotes in string?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17897651/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com