Regular Expressions in Java
Regular Expressions or Regex (in short) is an API for defining String patterns that can be used for searching, manipulating and editing a text. It is widely used to define constraint on strings such as password. Regular Expressions are provided under java.util.regex package.

The java.util.regex package primarily consists of the following three classes:
| Class | Description |
|---|---|
| util.regex.Pattern | Used for defining patterns |
| util.regex.Matcher | Used for performing match operations on text using patterns |
| PatternSyntaxException | Used for indicating syntax error in a regular expression pattern |
java.util.regex.Pattern Class
| Modifier and Type | Method | Description |
|---|---|---|
| static Pattern | compile(String regex) | It is used to compile the given regular expression into a pattern. |
| static Pattern | compile(String regex, int flags) | It is used to compile the given regular expression into a pattern with the given flags. |
| int | flags() | It is used to return this pattern’s match flags. |
| Matcher | matcher(CharSequence input) | It is used to create a matcher that will match the given input against this pattern. |
| static boolean | matches(String regex, CharSequence input) | It is used to compile the given regular expression and attempts to match the given input against it. |
| String | pattern() | It is used to return the regular expression from which this pattern was compiled. |
| static String | quote(String s) | It is used to return a literal pattern String for the specified String. |
| String[] | split(CharSequence input) | It is used to split the given input sequence around matches of this pattern. |
| String[] | split(CharSequence input, int limit) | It is used to split the given input sequence around matches of this pattern. |
| String | toString() | It is used to return the string representation of this pattern. |
// A Simple Java program to demonstrate working of // Pattern.matches() in Java import java.util.regex.Pattern; class Demo { public static void main(String args[]) { // Following line prints "true" because the whole // text "geeksforgeeks" matches pattern "geeksforge*ks" System.out.println (Pattern.matches("geeksforge*ks", "geeksforgeeks")); // Following line prints "false" because the whole // text "geeksfor" doesn't match pattern "g*geeks*" System.out.println (Pattern.matches("g*geeks*", "geeksfor")); } } |
java.util.regex.Matcher Class
| Modifier and Type | Method | Description |
|---|---|---|
| boolean | find() | It is mainly used for searching multiple occurrences of the regular expressions in the text. |
| boolean | find(int start) | It is used for searching occurrences of the regular expressions in the text starting from the given index. |
| int | start() | It is used for getting the start index of a match that is being found using find() method. |
| int | end() | It is used for getting the end index of a match that is being found using find() method. It returns index of character next to last matching character. |
| int | groupCount() | It is used to find the total number of the matched subsequence. |
| String | group() | It is used to find the matched subsequence. |
| boolean | matches() | It is used to test whether the regular expression matches the pattern. |
Note that Pattern.matches() checks if whole text matches with a pattern or not. Other methods (demonstrated below) are mainly used to find multiple occurrences of pattern in text.
Java Programs to demonstrate workings of compile(), find(), start(), end() and split() :
- Java Program to demonstrate simple pattern searching
// A Simple Java program to demonstrate working of// String matching in Javaimportjava.util.regex.Matcher;importjava.util.regex.Pattern;classDemo{publicstaticvoidmain(String args[]){// Create a pattern to be searchedPattern pattern = Pattern.compile("geeks");// Search above pattern in "geeksforgeeks.org"Matcher m = pattern.matcher("geeksforgeeks.org");// Print starting and ending indexes of the pattern// in textwhile(m.find())System.out.println("Pattern found from "+ m.start() +" to "+ (m.end()-1));}}chevron_rightfilter_noneOutput:
Pattern found from 0 to 4 Pattern found from 8 to 12
- Java Program to demonstrate simple regular expression searching
// A Simple Java program to demonstrate working of// String matching in Javaimportjava.util.regex.Matcher;importjava.util.regex.Pattern;classDemo{publicstaticvoidmain(String args[]){// Create a pattern to be searchedPattern pattern = Pattern.compile("ge*");// Search above pattern in "geeksforgeeks.org"Matcher m = pattern.matcher("geeksforgeeks.org");// Print starting and ending indexes of the pattern// in textwhile(m.find())System.out.println("Pattern found from "+ m.start() +" to "+ (m.end()-1));}}chevron_rightfilter_noneOutput:
Pattern found from 0 to 2 Pattern found from 8 to 10 Pattern found from 16 to 16
- Java program to demonstrate Case Insensitive Searching
// A Simple Java program to demonstrate working of// String matching in Javaimportjava.util.regex.Matcher;importjava.util.regex.Pattern;classDemo{publicstaticvoidmain(String args[]){// Create a pattern to be searchedPattern pattern = Pattern.compile("ge*", Pattern.CASE_INSENSITIVE);// Search above pattern in "geeksforgeeks.org"Matcher m = pattern.matcher("GeeksforGeeks.org");// Print starting and ending indexes of the pattern// in textwhile(m.find())System.out.println("Pattern found from "+ m.start() +" to "+ (m.end()-1));}}chevron_rightfilter_noneOutput:
Pattern found from 0 to 2 Pattern found from 8 to 10 Pattern found from 16 to 16
- Java program to demonstrate working of split() to split a text based on a delimiter pattern
// Java program to demonstrate working of splitting a text by a// given patternimportjava.util.regex.Matcher;importjava.util.regex.Pattern;classDemo{publicstaticvoidmain(String args[]){String text ="geeks1for2geeks3";// Specifies the string pattern which is to be searchedString delimiter ="\\d";Pattern pattern = Pattern.compile(delimiter,Pattern.CASE_INSENSITIVE);// Used to perform case insensitive search of the stringString[] result = pattern.split(text);for(String temp: result)System.out.println(temp);}}chevron_rightfilter_noneOutput:
geeks for geeks
MatchResult Interface
| Modifier and Type | Method | Description |
|---|---|---|
| int | end() | It is used to return the offset after the last character matched. |
| int | end(int group) | It is used to return the offset after the last character of the subsequence captured by the given group during this match. |
| String | group() | It is used to return the input subsequence matched by the previous match. |
| String | group(int group) | It is used to return the input subsequence captured by the given group during the previous match operation. |
| int | groupCount() | It is used to return the number of capturing groups in this match result’s pattern. |
| int | start() | It is used to return the start index of the match. |
| int | start(int group) | It is used to return the start index of the subsequence captured by the given group during this match. |
PatternSyntaxException Class
| Modifier and Type | Method | Description |
|---|---|---|
| String | getDescription() | It is used to retrieve the description of the error. |
| int | getIndex() | It is used to retrieve the error index. |
| String | getMessage() | It is used to return a multi-line string containing the description of the syntax error and its index, the erroneous regular-expression pattern, and a visual indication of the error index within the pattern. |
| String | getPattern() | It is used to retrieve the erroneous regular-expression pattern. |
Important Observations/Facts:
- We create a pattern object by calling Pattern.compile(), there is no constructor. compile() is a static method in Pattern class.
- Like above, we create a Matcher object using matcher() on objects of Pattern class.
- Pattern.matches() is also a static method that is used to check if given text as a whole matches pattern or not.
- find() is used to find multiple occurrences of pattern in text.
- We can split a text based on a delimiter pattern
using split()
Articles based on Java Regular Expression:
- Program to remove consonants from a String
- Check if a string contains only alphabets in Java using Regex
- Parsing Apache access log in Java
- Java | Removing whitespaces using RegexJava program to read all mobile numbers present in given file
- Program to check valid mobile number
- Check if URL is valid or not in Java
- Check if email address valid or not in Java
- Java.lang.String.matches() in Java
- Check if a binary string has a 0 between 1s or not | Set 2 (Regular Expression Approach)
- Extract maximum numeric value from a given string | Set 2 (Regex approach)
- Find all the patterns of “1(0+)1” in a given string | SET 2(Regular Expression Approach)
- Get the first letter of each word in a string using regex in Java
- Print first letter of each word in a string using regex
- Extracting each word from a String using Regex in Java
- Program to remove vowels from a String
- Regex Boundary Matchers in Java
- Split() String method in Java with examples
- Quantifiers in Java
Recent articles on Java Regular Expressions
This article is contributed by Akash Ojha. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Recommended Posts:
- Lambda Expressions in Java 8
- SQL | Conditional Expressions
- Lambda Expressions | Concurrent Programming Approach 4
- Area of a circle inscribed in a regular hexagon
- Find all the patterns of "1(0+)1" in a given string | SET 2(Regular Expression Approach)
- Check if a binary string has a 0 between 1s or not | Set 2 (Regular Expression Approach)
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- Java.util.concurrent.Phaser class in Java with Examples
- Java.util.Collections.rotate() Method in Java with Examples
- Java.util.LinkedList.peek() , peekfirst(), peeklast() in Java
- Java.util.LinkedList.offer(), offerFirst(), offerLast() in Java
- Java.util.Collections.disjoint() Method in java with Examples
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java lang.Long.byteValue() method in Java with Examples



