Matcher quoteReplacement(String s) 方法在 Java 中帶有示例
Matcher 類的 appendReplacement() 方法接受一個 StringBuffer 物件和一個 String(替換字串)作為引數,並將輸入資料附加到 StringBuffer 物件,使用替換字串替換匹配的內容。
在內部,此方法從輸入字串中讀取每個字元並附加字串緩衝區,每當發生匹配時,它會將替換字串附加到緩衝區中而不是替換字串中匹配內容的一部分,並且從匹配子字串的下一個位置繼續。
如果在此方法中傳遞替換字串時使用 “/” 或 “$”,則它們不會被視為常規字元並且會出現異常 −
示例 1
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class QuoteReplacement {
public static void main(String[] args) {
String str = " <p>This <b>is</b> an <b>example</b> HTML <b>script</b>.</p>";
//Regular expression to match contents of the bold tags
String regex = "<b>(\S+)</b>";
System.out.println("Input string: \n"+str);
//Creating a pattern object
Pattern pattern = Pattern.compile(regex);
//Matching the compiled pattern in the String
Matcher matcher = pattern.matcher(str);
//Creating an empty string buffer
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(sb, "sampledata$" );
//Matcher.quoteReplacement("Bo$ld/Data$"));
}
matcher.appendTail(sb);
System.out.println("Contents of the StringBuffer: \n"+ sb.toString() );
}
}輸出
Input string:<p>This <b>is</b> an <b>example</b> HTML <b>script</b>.</p>
Exception in thread "main" java.lang.IllegalArgumentException: Illegal group reference: group index is missing at java.util.regex.Matcher.appendReplacement(Unknown Source) at OCTOBER.matcher.QuoteReplacement.main(QuoteReplacement.java:18)
Matcher 類的 quote Replacement 方法接受字串值並返回一個文字替換字串。即,忽略給定字串中的字元 / 和 $,並且結果可以用作 appendReplacement() 方法的引數。
示例 2
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class QuoteReplacement {
public static void main(String[] args) {
String str = "<p>This <b>is</b> an <b>example</b> HTML <b>script</b>.</p>";
//Regular expression to match contents of the bold tags
String regex = "<b>(\S+)</b>";
System.out.println("Input string: \n"+str);
//Creating a pattern object
Pattern pattern = Pattern.compile(regex);
//Matching the compiled pattern in the String
Matcher matcher = pattern.matcher(str);
//Creating an empty string buffer
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(sb, Matcher.quoteReplacement("Bo$ld/Data$"));
}
matcher.appendTail(sb);
System.out.println("Contents of the StringBuffer: \n"+ sb.toString() );
}
}輸出
Input string: <p>This <b>is</b> an <b>example</b> HTML <b>script</b>.</p> Contents of the StringBuffer: <p>This Bo$ld/Data$ an Bo$ld/Data$ HTML Bo$ld/Data$.</p>
示例 3
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class QuoteReplacementExample {
public static void main(String[] args) {
String input = "This is sample text";
String regex = "[#]";
//Creating a pattern object
Pattern pattern = Pattern.compile(regex);
//Matching the compiled pattern in the String
Matcher matcher = pattern.matcher(input);
//Creating an empty string buffer
String str = Matcher.quoteReplacement("sampledata");
System.out.println(str);
}
}輸出
sampledata
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
安卓
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP