簡體   English   中英

如何雙引號雙引號?

[英]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\"

因此,正則表達式必須檢查雙引號之前是否有數字,以及是否還沒有轉義字符...

謝謝!!

嘗試使用Positive Lookbehind

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM