Pattern compile(String) method in Java with Examples Last Updated : 03 Apr, 2023 Comments Improve Suggest changes Like Article Like Report The 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 compiled 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 // 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.*"; // create 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); } } Output: actualString contains REGEX = true Program 2: Java // 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"; // create 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); } } Output: actualString contains REGEX = false Reference: https://docs.oracle.com/javase/10/docs/api/java/util/regex/Pattern.html#compile(java.lang.String) Comment More infoAdvertise with us Next Article Pattern compile(String) method in Java with Examples A AmanSingh2210 Follow Improve Article Tags : Java Java-Functions Java 8 Java-Pattern Practice Tags : Java Similar Reads Pattern compile(String,int) method in Java with Examples The compile(String, int) method of the Pattern class used to create a pattern from the regular expression with the help of flags where both expression and flags are passed as parameters to the method. The Pattern class contains a list of flags (int constants) that can be helpful to make the Pattern 2 min read Pattern toString() Method in Java with Examples toString() method of a Pattern class used to return the string representation of this pattern. This return the regular expression from which this pattern was compiled. Syntax: public String toString() Parameters: This method accepts nothing as parameter. Return value: This method returns a string re 1 min read Pattern quote(String) method in Java with Examples quote(String) method of a Pattern class used to returns a literal pattern String for the specified String passed as parameter to method.This method produces a String equivalent to s that can be used to create a Pattern. Metacharacters or escape sequences in the input sequence will be given no specia 3 min read CompositeName toString() method in Java with Examples The toString() method of a javax.naming.CompositeName class is used to create the string representation of this composite name object, using the syntax "/" as a separator of each object. The string representation returned by this composite name consists of enumerating in order each component of this 2 min read CompositeName toString() method in Java with Examples The toString() method of a javax.naming.CompositeName class is used to create the string representation of this composite name object, using the syntax "/" as a separator of each object. The string representation returned by this composite name consists of enumerating in order each component of this 2 min read Like