英文:
Java - Escape double quotes only if not already escaped
问题
给定一个字符串作为输入,我想转义所有那些本应该被转义但实际上没有被转义的双引号。例如,对于给定的输入:
"<?xmlversion=\"1.0"encoding="UTF-8\"?>"
,
输出应为:"<?xmlversion=\"1.0\"encoding=\"UTF-8\"?>"
。
我考虑使用正则表达式,但不太确定如何操作。如果有人能帮忙,我将不胜感激。
英文:
Given a String as input, I want to escape all those double quotes that should have escaped but they did not. For instance, given "<?xmlversion=\"1.0"encoding="UTF-8\"?>"
as input, the output should be "<?xmlversion=\"1.0\"encoding=\"UTF-8\"?>"
. I was thinking of using Regex but was not fully sure how. I would appreciate if someone could help with that.
答案1
得分: 0
你可以在这里使用正则表达式替换:
<!-- 语言:java -->
String xml = "<?xmlversion=\\\"1.0\"encoding=\"UTF-8\\\"?>";
System.out.println(xml);
xml = xml.replaceAll("(?<!\\\\)\"", "\\\\\"");
System.out.println(xml);
这将输出:
<!-- 语言:xml -->
<?xmlversion=\"1.0"encoding="UTF-8\"?>
<?xmlversion=\"1.0\"encoding=\"UTF-8\"?>
这里使用的正则表达式模式是:
(?<!\\)"
这将匹配所有不在反斜杠前面的双引号,并将它们替换为\"
。请注意,在Java的正则表达式语言中,我们必须对反斜杠进行双重转义。此外,文字双引号在Java字符串中变为\"
。
英文:
You could using a regex replacement here:
<!-- language: java -->
String xml = "<?xmlversion=\\\"1.0\"encoding=\"UTF-8\\\"?>";
System.out.println(xml);
xml = xml.replaceAll("(?<!\\\\)\"", "\\\\\"");
System.out.println(xml);
This prints:
<!-- language: xml -->
<?xmlversion=\"1.0"encoding="UTF-8\"?>
<?xmlversion=\"1.0\"encoding=\"UTF-8\"?>
The regex pattern used here is:
(?<!\\)"
This will target all double quotations which are not preceded by a backslash, and replace them with \"
. Note that we have to double escape the backslashes within Java's regex language. Also literal double quote becomes \"
in a Java string.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论