[英]How to double escape double quote?
假设我在Java中有这个String:
This a "text". This is inches 30". And another 20\"
我想查找所有英寸(数字后加双引号),并为其添加第二个转义符(如果还没有)。
因此,更改后,字符串将如下所示,打印在控制台中:
This is a "text". This is inches 30\". And another 20\"
因此,正则表达式必须检查双引号之前是否有数字,以及是否还没有转义字符...
谢谢!!
String str="This a \"text\". This is inches 30\". And another 20\\\"";
System.out.println(str.replaceAll("(?<=\\d)\"", "\\\\\""));
输出:
This a "text". This is inches 30\". And another 20\"
这也是在线演示 。
模式说明:
(?<= look behind to see if there is:
\d digits (0-9)
) end of look-behind
\" '"'
x='This a "text". This is inches 30". And another 20\"'
print re.sub(r"(\d+)(\")",r"\1\\\2",x)
这在python中。我不知道java的确切匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.