Pattern compile(String) method in Java with Examples
Thw compile(String) method of the Pattern class in Java is used to create a pattern from the regular expression passed as parameter to method. Whenever you need to match a text against a regular expression pattern more than one time, create a Pattern instance using the Pattern.compile() method.
Syntax:
public static Pattern compile(String regex)
Parameters: This method accepts one single parameter regex which represents the given regular expression compiled into a pattern.
Return Value: This method returns the pattern complied from the regex passed to the method as a parameter.
Exception: This method throws following exception:
- PatternSyntaxException: This exception is thrown if the expression’s syntax is invalid.
Below programs illustrate the compile(String) method:
Program 1:
// 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 = ".*www.*"; // creare the string // in which you want to search String actualString = "www.geeksforgeeks.org"; // compile the regex to create pattern // using compile() method Pattern pattern = Pattern.compile(REGEX); // get a matcher object from pattern Matcher matcher = pattern.matcher(actualString); // check whether Regex string is // found in actualString or not boolean matches = matcher.matches(); System.out.println("actualString " + "contains REGEX = " + matches); } } |
actualString contains REGEX = true
Program 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 = "brave"; // creare the string // in which you want to search String actualString = "Cat is cute"; // compile the regex to create pattern // using compile() method Pattern pattern = Pattern.compile(REGEX); // check whether Regex string is // found in actualString or not boolean matches = pattern .matcher(actualString) .matches(); System.out.println("actualString " + "contains REGEX = " + matches); } } |
actualString contains REGEX = false
Reference: https://docs.oracle.com/javase/10/docs/api/java/util/regex/Pattern.html#compile(java.lang.String)
Recommended Posts:
- Pattern pattern() method in Java with Examples
- Pattern toString() Method in Java with Examples
- Pattern splitAsStream() Method in Java with Examples
- Matcher pattern() method in Java with Examples
- Pattern flags() method in Java with Examples
- Pattern asPredicate() Method in Java with Examples
- Pattern compile(String,int) method in Java with Examples
- Matcher usePattern(Pattern) method in Java with Examples
- Pattern split(CharSequence) method in Java with Examples
- Pattern quote(String) method in Java with Examples
- Pattern split(CharSequence,int) method in Java with Examples
- Pattern matcher(CharSequence) method in Java with Examples
- Pattern matches(String ,CharSequence) method in Java with Examples
- Java lang.Long.builtcount() method in Java with Examples
- Java lang.Long.reverse() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



