Matcher.pattern() 方法在 Java 正則表示式中
java.time.Matcher.pattern() 方法返回 Matcher 匹配的模式。此方法不接受任何引數。
以下給出一個演示在 Java 正則表示式中 Matcher.pattern() 方法的程式 −
示例
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo { public static void main(String args[]) { String regex = "Apple"; Pattern p = Pattern.compile(regex); Matcher m = p.matcher("AppleIsAFruit"); System.out.println("Pattern: " + m.pattern()); } }
上述程式的輸出如下 −
Pattern: Apple
現在讓我們來理解一下上面的程式。
使用 Matcher.pattern() 方法返回 Matcher 匹配的模式,然後打印出來。以下程式碼片段演示了這一點 −
String regex = "Apple"; Pattern p = Pattern.compile(regex); Matcher m = p.matcher("AppleIsAFruit"); System.out.println("Pattern: " + m.pattern());
廣告