简体   繁体   English

如何双引号双引号?

[英]How to double escape double quote?

Say I have this String in java: 假设我在Java中有这个String:

This a "text". This is inches 30". And another 20\"

I want to find all inches (double quotes after a number) and add a second escape for it, if it doesn't have yet. 我想查找所有英寸(数字后加双引号),并为其添加第二个转义符(如果还没有)。

So after the change, the string would be like this, printed in console: 因此,更改后,字符串将如下所示,打印在控制台中:

This is a "text". This is inches 30\". And another 20\"

So the regex must check if there is a number before the double quote and if there is not a escape character already... 因此,正则表达式必须检查双引号之前是否有数字,以及是否还没有转义字符...

thanks!! 谢谢!!

Try with Positive Lookbehind 尝试使用Positive Lookbehind

String str="This a \"text\". This is inches 30\". And another 20\\\"";
System.out.println(str.replaceAll("(?<=\\d)\"", "\\\\\""));

output: 输出:

This a "text". This is inches 30\". And another 20\"

Here is Online demo as well. 这也是在线演示

Pattern explanation: 模式说明:

  (?<=                     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)

This is in python.I dont know the exact match for java. 这在python中。我不知道java的确切匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM