【发布时间】:2020-06-19 04:17:29
【问题描述】:
我希望将字符串中出现的所有转义引号 (\") 替换为 (\\\"),然后将所有剩余的未转义引号 (") 替换为转义引号 (\")。到目前为止,这是我尝试过的:
row = row.replaceAll("\\\\(?>\")", "\\\\\"");
row = row.replaceAll("((?<!\\\\)\")", "\"");
示例输入:
"This is a test with \" and "'s where \" is replaced with triple \'s before "
示例输出:\"This is a test with \\\" and \"'s where \\\" is replaced with triple \'s before \"
\\(?>\")" 在 https://www.freeformatter.com/java-regex-tester.html#ad-output 上工作,在 replaceAll 中找不到转义引号。
对此的任何帮助表示赞赏。
【问题讨论】:
-
@Mandy8055 不,它捕获了所有的 \,而不仅仅是那些前缀引号的。所以“This is a test with \ and \” ...”都被替换了。