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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。