Java中Pattern類的pattern()方法用於獲取正則表達式,將其編譯以創建此模式。我們使用正則表達式創建模式,並且使用此方法來獲取相同的源表達式。
用法:
public String pattern()
參數:此方法不接受任何內容作為參數。
返回值:此方法返回模式的源正則表達式。
以下示例程序旨在說明pattern()方法:
示例1:
// Java program to demonstrate
// Pattern.pattern() method
import java.util.regex.*;
public class GFG {
public static void main(String[] args)
{
// create a REGEX String
String REGEX = "(.*)(for)(.*)?";
// create the string
// in which you want to search
String actualString
= "code of Machine";
// create pattern
Pattern pattern1 = Pattern.compile(REGEX);
// find the regular expressio of pattern
String RegularExpression = pattern1.pattern();
System.out.println("Pattern's RegularExpression = "
+ RegularExpression);
}
}
輸出:
Pattern's RegularExpression = (.*)(for)(.*)?
示例2:
// Java program to demonstrate
// Pattern.compile method
import java.util.regex.*;
public class GFG {
public static void main(String[] args)
{
// create a REGEX String
String REGEX = "(.*)(ee)(.*)?";
// create the string
// in which you want to search
String actualString
= "geeks";
// create pattern
Pattern pattern1 = Pattern.compile(REGEX);
// find the regular expressio of pattern
String RegularExpression = pattern1.pattern();
System.out.println("Pattern's RegularExpression = "
+ RegularExpression);
}
}
輸出:
Pattern's RegularExpression = (.*)(ee)(.*)?
參考文獻:
https://docs.oracle.com/javase/10/docs/api/java/util/regex/Pattern.html#pattern()
相關用法
- Java Pattern toString()用法及代碼示例
- Java Pattern asPredicate()用法及代碼示例
- Java Pattern flags()用法及代碼示例
- Java Matcher pattern()用法及代碼示例
- Java Pattern splitAsStream()用法及代碼示例
- Java Pattern compile(String,int)用法及代碼示例
- Java Pattern matcher(CharSequence)用法及代碼示例
- Java Pattern quote(String)用法及代碼示例
- Java Pattern split(CharSequence,int)用法及代碼示例
- Java Pattern compile(String)用法及代碼示例
- Java Pattern split(CharSequence)用法及代碼示例
- Java Matcher usePattern(Pattern)用法及代碼示例
- Java Pattern matches(String ,CharSequence)用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 Pattern pattern() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。