我正試圖替換一個字串的一部分,如果該值在%符號之內。
因此,例如下面的字串。
other text%original text%other text
替換之后,我應該得到
other textreplaced textother text
我能夠通過以下方式實作這一目標:
String newStr = "other text%original text%other text" /span>. replaceFirst("%.*%", "被替換文本")。
但是,%符號并不總是保證以%的形式出現。它可能是ASCII格式的,如下所示。
other text%原始文本%其他文本
現在我可以像下面這樣寫兩次。
我可以寫兩次。
String toBeReplaced = //
String newStr = toBeReplaced.replaceFirst("%.*%", "被替換文本") 。
newStr = "other toBeReplaced.replaceFirst("% 。 *%", "替換的文本")。)
但是,我是否有辦法修改這個詞組,以便我可以只執行一次替換,以覆寫這兩種情況?
謝謝你。
uj5u.com熱心網友回復:
在java中試一下這個regex,other textreplaced textother text是結果。
String str1 = "other text%original text%other text" /span>。
System.out.println(str1.replaceAll("\%.*?%", "被替換文本"))。
String str2 = "other text%original text& #37;other text"。
System.out.println(str2.replaceAll("%.*?%", "被替換文本"))。
System.out.println(str1.replaceAll("(`%.*?%)|(`%.*?%)", "替換的文本")。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/307536.html
標籤:
上一篇:有多個值的本體
