0

我有一个带有外部双引号的字符串""。我需要删除内部双引号。什么是正则表达式?

例如:

输入:“你好,今晚我到了”---> 输出:“你好,我今晚到了”
输入:“你好,我今晚到了””---> 输出:“你好,我今晚到了”
输入: “你好,我今晚到了”---> 输出:“你好,我今晚到了”

我尝试了以下代码,但它不适用于我的示例 2 和 3。它适用于示例 1。

data.replaceAll("\"(\\b[^\"]+)?\"\\b([^\"]+)\\b\"([^\"]+\\b)?\"","\"$1$2$3\"");

背景:我有一个 CSV 文件,我需要对其进行解析。数据带有外部引号内的内部引号。例如:

"aa","bb","cc","dd "REMOVE QUOTES" "
"aaa","bbb","ccc",""REMOVE QUOTES" ddd "

我希望正则表达式仅删除内部引号并保留外部引号。输出:

"aa","bb","cc","dd REMOVE QUOTES "
"aaa","bbb","ccc","REMOVE QUOTES ddd "
4

3 回答 3

2

You could use a combination of negative look-behind and look-ahead:

data = data.replaceAll("(?<!^)\"(?!$)", "")
  • (?<!^) Negative look-behind for start of line
  • (?!$) Negative look-ahead for EOL
于 2013-04-24T01:19:59.627 回答
1

试一试。我得到内部字符串(忽略外部引号)然后删除所有引号并添加外部引号。

String input1 = "\"Hello there \"I arrive\" tonight\"";
String output1 = "\"" + input1.substring(1, input1.length() - 1).replaceAll("\"", "") + "\"";
System.out.println(output1);

输出

“你好我今晚到”

于 2013-04-24T01:23:44.323 回答
0

如果我假设您的字符串以第一个 " 开头并以最后一个 " 结束,您可以将其用于正则表达式:

data.replaceAll("(?<!^)\"(?!$)", "");
于 2013-04-24T01:35:46.153 回答