Pattern compile(String,int) method in Java with Examples Last Updated : 06 Feb, 2023 Comments Improve Suggest changes Like Article Like Report 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 matching behave in certain ways. For example, The flag name CASE_INSENSITIVE is used to ignore the case of the text at the time of matching.Syntax: public static Pattern compile(String regex, int flags) Parameters: This method accepts two parameters: regex: This parameter represents the given regular expression compiled into a pattern.flag: This parameter is an integer representing Match flags, a bit mask that may include CASE_INSENSITIVE, MULTILINE, DOTALL, UNICODE_CASE, CANON_EQ, UNIX_LINES, LITERAL, UNICODE_CHARACTER_CLASS and COMMENTS. Return Value: This method returns the pattern compiled from passed regex and flags.Exception: This method throws following exceptions: PatternSyntaxException: This exception is raised if the expression's syntax is invalid.IllegalArgumentException: This exception is raised if bit values other than those corresponding to the defined match flags are set in flags. Below programs illustrate the compile(String, int) 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 = "(.*)(for)(.*)?"; // create the string // in which you want to search String actualString = "code of Machine"; // compile the regex to create pattern // using compile() method Pattern pattern = Pattern.compile(REGEX, Pattern.CASE_INSENSITIVE); // 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 Time Complexity : O(N) Space Complexity : O(1) 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 = ".*org.*"; // create the string // in which you want to search String actualString = "geeksforgeeks.org"; // compile the regex to create pattern // using compile() method Pattern pattern = Pattern.compile(REGEX, Pattern.CASE_INSENSITIVE); // 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 = true Time Complexity : O(N) Space Complexity : O(1) References: https://docs.oracle.com/javase/10/docs/api/java/util/regex/Pattern.html#compile(java.lang.String, int) Comment More infoAdvertise with us Next Article Pattern compile(String,int) 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) method in Java with Examples 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.Synta 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 Class toString() method in Java with Examples The toString() method of java.lang.Class class is used to convert the instance of this Class to a string representation. This method returns the formed string representation. Syntax: public String toString() Parameter: This method does not accept any parameter. Return Value: This method returns the 1 min read String toString() Method in java with Examples String toString() is the built-in method of java.lang which return itself a string. So here no actual conversion is performed. Since toString() method simply returns the current string without any changes, there is no need to call the string explicitly, it is usually called implicitly. Syntax : publ 1 min read Class toGenericString() method in Java with Examples The toGenericString() method of java.lang.Class class is used to convert the instance of this Class to a string representation along with the information about the modifiers and type parameters. This method returns the formed string representation. Syntax: public String toGenericString() Parameter: 2 min read Like