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.
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 complied 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:
actualString contains REGEX = false
actualString contains REGEX = true
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Pattern pattern() method in Java with Examples
- Pattern splitAsStream() Method in Java with Examples
- Matcher pattern() method in Java with Examples
- Matcher usePattern(Pattern) method in Java with Examples
- Pattern compile(String) method in Java with Examples
- Pattern flags() method in Java with Examples
- Pattern matches(String ,CharSequence) method in Java with Examples
- Pattern matcher(CharSequence) method in Java with Examples
- Pattern quote(String) method in Java with Examples
- Pattern split(CharSequence) method in Java with Examples
- Pattern split(CharSequence,int) method in Java with Examples
- Pattern toString() Method in Java with Examples
- Pattern asPredicate() Method in Java with Examples
- Pattern Occurrences : Stack Implementation Java
- Builder Pattern in java
- Printing Triangle Pattern in Java
- Singleton Design Pattern | Implementation
- Singleton Design Pattern | Introduction
- How to prevent Singleton Pattern from Reflection, Serialization and Cloning?
- Program to print interesting pattern
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.
Improved By : Akanksha_Rai