Matcher pattern() method in Java with Examples
The pattern() method of Matcher Class is used to get the pattern to be matched by this matcher.
Syntax:
public Pattern pattern()
Parameters: This method do not accepts any parameter.
Return Value: This method returns a Pattern which is the pattern to be matched by this Matcher.
Below examples illustrate the Matcher.pattern() method:
Example 1:
import java.util.regex.*;
public class GFG {
public static void main(String[] args)
{
String regex = "Geeks" ;
Pattern pattern
= Pattern.compile(regex);
String stringToBeMatched
= "GeeksForGeeks" ;
Matcher matcher
= pattern.matcher(stringToBeMatched);
System.out.println( "Pattern: "
+ matcher.pattern());
}
}
|
Example 2:
import java.util.regex.*;
public class GFG {
public static void main(String[] args)
{
String regex = "GFG" ;
Pattern pattern
= Pattern.compile(regex);
String stringToBeMatched
= "GFGFGFGFGFGFGFGFGFG" ;
Matcher matcher
= pattern.matcher(stringToBeMatched);
System.out.println( "Pattern: "
+ matcher.pattern());
}
}
|
Reference: Oracle Doc
Last Updated :
26 Nov, 2018
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...