Javatpoint Logo
Javatpoint Logo

Java Pattern compile() Method

The compile() method of Pattern class is used to compile the given regular expression passed as the string. It used to match text or expression against a regular expression more than one time.

Signature

SN Method Description
1 static Pattern compile(String regexp) Return compiled version of a regular expression into the pattern.
2 static Pattern compile(String regexp, int flags) Return compiled version of a regular expression into the pattern with the given flag.

Parameter

regexp - The expression that we want to compile.

flags - Match flags, a bit mask that may include

Returns

The given regular expression compiled into a pattern

Throws

PatternSyntaxException - If the expression's syntax is invalid

Example 1

Test it Now

Output:

Result :  java.util.regex.Matcher[pattern=@gmail region=0,13 lastmatch=@gmail]

Example 2

Test it Now

Output:

Start : 3, End : 9 Pattern group : @gmail
 Start : 12, End : 18 Pattern group : @gmail
 Start : 21, End : 27 Pattern group : @gmail
 number of Matches:  3

Example 3

Test it Now

Output:

Result :  java.util.regex.Matcher[pattern=@Gmail region=0,13 lastmatch=@gmail]

Example 4

Test it Now

Output:

Start : 0, End : 1 Pattern group : 
 Start : 4, End : 5 Pattern group : 
 number of Matches:  2
Next TopicJava Regex





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA