Java 中的 Matcher appendTail() 方法(附帶示例)
java.util.regex.Matcher 類表示執行各種匹配操作的引擎。此類沒有建構函式,你可以使用 java.util.regex.Pattern 類的 matches() 方法建立/獲取此類的物件。
此 (Matcher) 類的 appendTail() 方法接受 StringBuffer 物件並向其附加輸入序列的字元。
示例
import java.util.regex.Matcher; import java.util.regex.Pattern; public class AppendTail { 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>"; //Creating a pattern object Pattern pattern = Pattern.compile(regex); //Matching the compiled pattern in the String Matcher matcher = pattern.matcher(str); StringBuffer sb = new StringBuffer(); matcher.appendTail(sb); while (matcher.find()) { System.out.println(matcher.group(1)); } System.out.println("Contents of the StringBuffer: \n"+ sb); } }
輸出
is example script Contents of the StringBuffer: <p>This <b>is</b> an <b>example</b> HTML <b>script</b>.</p>
廣告