Java regular expression tutorial with examples (regex) will help you understand how to use the regular expressions in Java. Java regular expressions are sometimes also called Java regex and it is a powerful way to find, match, and extract data from character sequence.
There are two main classes in the java.util.regex package namely Pattern and Matcher class. The Pattern represents a compiled regular expression while the Matcher is an engine that matches character sequence with the pattern. If the pattern has a syntax error, the PatternSyntaxException is thrown to indicate that.
I have divided this Java regular expression tutorial (regex tutorial) into three parts understanding the Pattern class, understanding the Matcher class, and how to create the actual regular expression patterns to find and extract the data.
1. The Pattern class
The Pattern represents a compiled regular expression. The string containing regular expression must be compiled to the instance of the Pattern class. Once we have the instance of the Pattern class, we can then create a Matcher object to match the character sequence against this pattern.
How to compile a regular expression pattern using the compile method?
The static compile
method of the Pattern class compiles the given string regular expression into a pattern.
1 |
Pattern pattern = Pattern.compile("[0-9]+"); |
The above code compiles a regular expression into a pattern.
There is an overloaded compile
method that accepts special flags along with the regex.
1 |
public static Pattern compile(String regex, int flags) |
Here are some of the important static int fields defined by the Pattern class which can be mentioned as the flags.
Pattern.UNIX_LINES | This flag enables Unix line mode where only ‘\n’ is recognized as a line terminator. |
Pattern.CASE_INSENSITIVE | The default pattern matching is case sensitive. This flag enables case insensitive matching. |
Pattern.COMMENTS | This flag allows white spaces and comments in patterns. |
Pattern.MULTILINE | This flag enables the multiline mode. |
Pattern.LITERAL | This flat enables the literal parsing of the pattern. Any meta-characters or escape sequences will be treated as literal characters and will lose the special meanings. |
Pattern.DOTALL | This flag enables the “dotall” mode where the “.” (dot) character matches any character including the line terminator. By default, the line terminator is not matched. |
The Pattern flags
method returns the flags that were used to create this pattern object.
How to get the regex pattern used to create the Pattern object?
The Pattern pattern
method returns the string containing the regular expression which is used to create this pattern object. We can also use the toString
method to get the string representing the regular expression which was used to create this pattern object.
How to create a matcher from the pattern?
The Pattern matcher
method creates and returns a Matcher object that will match the specified character sequence against this pattern.
1 2 3 4 5 6 7 8 9 10 |
//the pattern we want to match Pattern pattern = Pattern.compile("[0-9]+"); //the string we want the pattern to match against Matcher matcher = pattern.matcher("12345"); if(matcher.matches()) System.out.println("Matched"); else System.out.println("Not matched"); |
Output
1 |
Matched |
How to match a pattern in a literal way?
Consider below given example.
1 2 3 4 5 6 7 |
Pattern pattern = Pattern.compile("|"); Matcher matcher = pattern.matcher("|"); if(matcher.matches()) System.out.println("Matched"); else System.out.println("Not matched"); |
Output
1 |
Not matched |
The string “|” did not match the pattern “|” even though it should. It is because there are several characters in the regular expressions that have special meanings. These characters are called metacharacters. The pipe character (|) is a meta-character and means OR in regular expression which is why it did not match.
If you want to match the pattern literally you can use the quote
method of the Pattern class. It returns a literal pattern string from the specified regular expression string.
1 2 3 4 5 6 7 |
Pattern pattern = Pattern.compile(Pattern.quote("|")); Matcher matcher = pattern.matcher("|"); if(matcher.matches()) System.out.println("Matched"); else System.out.println("Not matched"); |
Output
1 |
Matched |
How to split a string using the split method of the Pattern class?
The split
method of the Pattern class splits the given character sequence around the matches of this pattern. It returns an array of the String containing the parts.
1 2 3 4 |
Pattern pattern = Pattern.compile(","); String[] parts = pattern.split("One,Two,Three"); System.out.println(Arrays.toString(parts)); |
Output
1 |
[One, Two, Three] |
Please visit the full how to split a string example to know more.
2. The Matcher class
The Matcher class is a regex engine that matches the pattern with the given input string sequence. A matcher object can be created using the matcher
method of the Pattern class. Once created, the matcher object can be used to match the input string with the pattern.
The Matcher class provides 3 types of matching operations through matches
, find
, and lookingAt
methods.
How to match the entire string with a pattern using the matches method?
The Matcher matches
method returns true if the matcher’s pattern matches the entire input string.
1 |
public boolean matches() |
It returns true if and only if the entire input sequence matches the pattern.
1 2 3 4 5 6 7 8 |
Pattern pattern = Pattern.compile("cat"); Matcher matcher = pattern.matcher("cat"); /* * returns true as the entire input "cat" * matches with the pattern "cat" */ System.out.println( matcher.matches() ); |
Output
1 |
true |
The code given above is the same as calling the matches
method of the String class or the matches
method of the Pattern class.
1 2 3 4 5 |
//same as above code "cat".matches("cat"); //this is also same Pattern.matches("cat", "cat"); |
Please note that the matches
method matches the pattern against the entire input sequence. It returns false for the partial matches or no matches.
1 2 3 4 5 6 7 8 9 10 11 |
//true System.out.println( Pattern.matches("cat", "cat") ); //false, partial match System.out.println( Pattern.matches("cat", "cat&dog") ); //true, entire string matches System.out.println( Pattern.matches("cat.*", "cat&dog") ); //false, partial match System.out.println( Pattern.matches("dog", "cat&dog") ); |
Output
1 2 3 4 |
true false true false |
How to match a partial input string with a pattern using the find method?
The Matcher find
method finds the next subsequence or substring within the input string that matches the pattern.
1 |
public boolean find() |
The find
method starts matching the pattern at the beginning of the input string/region or at the first character that was not matched if the previous call to this method was successful.
If you want to find the subsequence starting from the specific position of the input string, you can use the overloaded find
method.
1 |
public boolean find(int start) |
It is the same as the find
method without any parameters but starts matching from the specified index instead of the beginning of the input string/region.
Unlike the matches
method, the find
method returns true for the partial matches.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
Pattern pattern = null; Matcher matcher = null; //true pattern = Pattern.compile("cat"); matcher = pattern.matcher("cat"); System.out.println( matcher.find() ); //true pattern = Pattern.compile("cat"); matcher = pattern.matcher("cat&dog"); System.out.println( matcher.find() ); //true pattern = Pattern.compile("&"); matcher = pattern.matcher("cat&dog"); System.out.println( matcher.find() ); //true pattern = Pattern.compile("dog"); matcher = pattern.matcher("cat&dog"); System.out.println( matcher.find() ); |
Output
1 2 3 4 |
true true true true |
If the find
method is able to match the pattern with the input sequence, we can get more information regarding the match using below given methods.
1 |
public int start() |
The Matcher start
method returns the starting index of the previous match.
1 |
public int end() |
The Matcher end
method returns the offset after the last character matched.
1 |
public String group() |
The Matcher group
method returns the substring that matched in the previous match.
1 |
public String group(int group) |
This overloaded group
method accepts the group number and returns the substring captured by the specified group during the last match. Group 0 represents the entire pattern so calling group(0)
is the same as calling the group
method without any parameters.
Let’s now see how to use these methods to extract the relevant information from the match operation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
Pattern pattern = Pattern.compile("cat"); Matcher matcher = pattern.matcher("cat&dog&cat"); while( matcher.find() ){ //this will return the matched substring System.out.println( matcher.group() ); //same as above //System.out.println( matcher.group(0) ); //this will return the start index of the match in the string System.out.println( "Starting at index: " + matcher.start() ); /* * this will return end offset of the match, * substract -1 from it to get the index */ System.out.println( "Ending at index: " + (matcher.end()-1) ); } |
Output
1 2 3 4 5 6 |
cat Starting at index: 0 Ending at index: 2 cat Starting at index: 8 Ending at index: 10 |
As we can see from the output, the find
method starts matching at the beginning of the input string initially. When we call it again after the successful match, it starts matching from the end of the previous match to find more matches.
There is an overloaded start
, end
and group
methods that accept a group number. I will explain these methods later in this tutorial under the capturing group section.
How to match the pattern with the input string using the lookingAt method?
The Matcher lookingAt
method matches the pattern with the input sequence/region at the beginning.
1 |
public boolean lookingAt() |
The lookingAt
method starts matching at the beginning of the input/region but does not require the entire input/region to be matched with the pattern.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
Pattern pattern = null; Matcher matcher = null; //true pattern = Pattern.compile("cat"); matcher = pattern.matcher("cat"); System.out.println( matcher.lookingAt() ); //true pattern = Pattern.compile("cat"); matcher = pattern.matcher("cat&dog"); System.out.println( matcher.lookingAt() ); //false, no match at the beginning pattern = Pattern.compile("&"); matcher = pattern.matcher("cat&dog"); System.out.println( matcher.lookingAt() ); //false, no match at the beginning pattern = Pattern.compile("dog"); matcher = pattern.matcher("cat&dog"); System.out.println( matcher.lookingAt() ); |
Output
1 2 3 4 |
true true false false |
Differences between the matches, find and lookingAt methods
The matches
and lookingAt
methods always start matching at the beginning of the input or region while the find
method does not.
The lookingAt
and find
methods match a subsequence or substring within the input or region, while the matches
method requires the entire input to be matched with the pattern.
The find
matches multiple substrings or subsequences matching with the given pattern. The matches
and lookingAt
method matches only once.
How to reuse the matcher using the reset method?
The Matcher reset
method resets this matcher object.
1 |
public Matcher reset() |
It discards all the state information and resets the position to 0. There is also an overloaded reset
method that accepts a new character sequence.
1 |
public Matcher reset(CharSequence input) |
This method resets this matcher object with the new input sequence. We can use it when we want to match multiple input sequences with the same compiled pattern as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Pattern pattern = Pattern.compile("cat"); Matcher matcher = pattern.matcher("cat"); System.out.println(matcher.matches()); /* * If you want to match a different string with * the same pattern, reset the matcher object * with the new sequence instead of creating * a new matcher object */ matcher.reset("cat&dog"); System.out.println(matcher.find()); |
Output
1 2 |
true true |
How to create a search region of the input string to match using the region method?
The Matcher region
method creates a region within the input sequence.
1 |
public Matcher region(int start, int end) |
In other words, the region
method sets the boundary for the matcher to find the matches within the input sequence. Calling this method will reset this matcher object and then set the region.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
Pattern pattern = Pattern.compile("cat"); Matcher matcher = pattern.matcher("cat&cat&dog"); while(matcher.find()){ System.out.println( "Match \"" + matcher.group() + "\"" + " starting from index " + matcher.start() + " to index " + (matcher.end() - 1)); } /* * This will reset the matcher first, and * then set the start and end boundary to * search the pattern within index 3 to 7 */ matcher.region(3, 7); System.out.println("Setting region from index 3 to 7"); while(matcher.find()){ System.out.println( "Match \"" + matcher.group() + "\"" + " starting from index " + matcher.start() + " to index " + (matcher.end() - 1)); } |
Output
1 2 3 4 |
Match "cat" starting from index 0 to index 2 Match "cat" starting from index 4 to index 6 Setting region from index 3 to 7 Match "cat" starting from index 4 to index 6 |
As we can see from the output, after setting the region, the find
method found only match in the specified region of the input string. This is useful when we want to find a match within a specific area of the input sequence.
We can get the start and end index of the region using the regionStart
and regionEnd
methods.
1 |
public int regionStart() |
The regionStart
method returns the start index of this matcher object’s region.
1 |
public int regionEnd() |
The regionEnd
method returns the end index of this matcher object’s region.
How to change the pattern of the matcher object using the usePattern method?
The Matcher usePattern
method changes the pattern of this matcher object.
1 |
public Matcher usePattern(Pattern newPattern) |
The usePattern
method changes the pattern used by this matcher object to find the matches.
Please note that this matcher’s position in the input sequence is maintained as-is. In other words, the new pattern finds the matches after the current position of this matcher object. To understand this, let’s see an example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
Pattern pattern = Pattern.compile("cat"); Matcher matcher = pattern.matcher("dog&cat&cat&dog"); if(matcher.find()){ System.out.println( "Match \"" + matcher.group() + "\"" + " starting from index " + matcher.start() + " to index " + (matcher.end() - 1)); /* * Change this matcher's pattern to a new pattern. This * will not change the current position of the matcher * object i.e. 7 (end of the last successful match) */ matcher.usePattern(Pattern.compile("dog")); /* * this will find "dog" only after the current position */ if(matcher.find()){ System.out.println( "Match \"" + matcher.group() + "\"" + " starting from index " + matcher.start() + " to index " + (matcher.end() - 1)); } } |
As we can see from the output, changing the pattern of the matcher object did not match the first substring “dog” that starts at index 0 but it searched the new pattern from the current position onwards.
Apart from these methods, the Matcher class also provides the replaceAll
and replaceFirst
methods that I have covered in the Java String tutorial.
3. Understanding the Regular Expression Patterns
In the first two parts, I have shown you how to create a pattern and obtain a matcher object to do various types of matching. In this part, we will dive deep into how to create different types of patterns to find and extract the data from the input string.
How to match a string literal?
The most basic type of matching is matching a string with another string like given below.
1 2 |
//will print true System.out.println( Pattern.matches("Java", "Java") ); |
Remember that the matches
method matches the whole string against the pattern so the below given pattern will not match.
1 2 |
//will print false System.out.println( Pattern.matches("Java", "JavaC++") ); |
What are the metacharacters?
The metacharacters are the characters with a special meaning in the regular expressions. Below given are the metacharacters supported by Java regex API.
^ | Matches the start of the input string |
$ | Matches the end of the input string |
. | Matches any character except for the new line character |
* | Matches the previous character or expression zero or more times |
+ | Matches the previous character or expression one or more times |
? | Matches the previous character or expression zero or one time |
() | Match grouping |
[] | Defines a character class |
\ | Used to escape metacharacters to make it a literal character for matching |
| | OR operator |
{} | The number of times the previous pattern needs to be matched. |
(?=RegEx) | Positive lookahead matching |
(?!RegEx) | Negative lookahead matching |
(?<=RegEx) | Positive lookbehind matching |
(?<!RegEx) | Negative lookbehind matching |
\t | Matches the tab character ‘\u0009’ |
\n | Matches the new line character ‘\u000A’ |
\r | Matches the carriage return character ‘\u000D’ |
\f | Matches the form feed character ‘\u000C’ |
\a | Matches the alert character ‘\u0007’ |
\e | Matches the escape character ‘\u001B’ |
\cx | Matches the control character corresponding to x |
\\ | Matches the backslash character |
\Q | Starts of the quote |
\E | Ends of the quote |
How to match at the start and end of the input string?
The ^ metacharacter matches at the start of the input string while the $ metacharacter matches at the end of the input string as given below.
1 2 3 4 5 |
//matches I at the start of the input string, will print true System.out.println( Pattern.matches("^I.*", "I Love Java") ); //matches Java at the end of the input string, will print true System.out.println( Pattern.matches(".*Java$", "Java") ); |
How to match any number of characters?
There are 4 metacharacters that control how many characters need to be matched and how many times. These metacharacters are “.”, “+”, “?” and “*”. The dot metacharacter matches any character in the input string.
1 2 3 4 5 6 7 8 9 10 11 |
//the . matches with any character, will print true System.out.println( Pattern.matches("J.va", "Java") ); //will print true System.out.println( Pattern.matches(".ava", "Java") ); //will print true System.out.println( Pattern.matches("J..a", "Java") ); //will print true System.out.println( Pattern.matches("...a", "Java") ); |
The “?” metacharacter matches the previous character or expression zero or one time.
1 2 3 4 5 6 7 8 |
/* * matches "book" followed by "s" zero or more times with "book" input string, * prints true as the "s" was found 0 times after "book" */ System.out.println( Pattern.matches("books?", "book") ); //prints true, as the "s" found 1 time after "book" System.out.println( Pattern.matches("books?", "books") ); |
The “+” metacharacter matches the previous character or expression one or more times.
1 2 3 4 5 6 7 8 9 10 11 |
//will print false, as the "1" is 3 times while ? denotes zero or one time System.out.println( Pattern.matches("1?", "111") ); /* * will print true as the + denotes one or more times, * the lower limit is one time while the upper limit is unlimited times */ System.out.println( Pattern.matches("1+", "111") ); //prints true as well System.out.println( Pattern.matches("1+", "1111111") ); |
The “*” metacharacter matches the previous character or expression zero or more times.
1 2 3 4 5 6 7 8 9 10 11 |
/* * The * denotes zero or more times, */ //will print true, as the 3 is zero times after 12 System.out.println( Pattern.matches("123*", "12") ); //will print true, as the 3 is one time after 12 System.out.println( Pattern.matches("123*", "123") ); //will print true as well as the upper limit is unlimited times System.out.println( Pattern.matches("123*", "12333") ); |
We can also combine the “.” with “?”, “+” and “*” metacharacters to match any character any number of times as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
/* * Using .* */ //matches any character after 12 any number of times, prints true System.out.println( Pattern.matches("12.*", "123") ); //prints true System.out.println( Pattern.matches("12.*", "1244") ); //prints true System.out.println( Pattern.matches("12.*", "12abc") ); //prints true System.out.println( Pattern.matches("12.*", "12") ); /* * Using .+ */ //matches any character one or more time after 12 System.out.println( Pattern.matches("12.+", "123") ); //prints true System.out.println( Pattern.matches("12.+", "1244") ); //prints true System.out.println( Pattern.matches("12.+", "12abc") ); //prints false, any character must occur at least one time System.out.println( Pattern.matches("12.+", "12") ); /* * Using .? */ //matches any character zero or one time after 12, will print true System.out.println( Pattern.matches("12.?", "123") ); //prints false, there 2 characters after 12 System.out.println( Pattern.matches("12.?", "1244") ); //prints false there are 3 characters after 12 System.out.println( Pattern.matches("12.?", "12abc") ); //prints true, there are zero characters after 12 System.out.println( Pattern.matches("12.?", "12") ); |
How to match using the OR (|) operator?
The “|” (pipe) metacharacter is used to denote the OR condition in the regular expressions.
1 2 3 4 5 6 7 8 9 10 11 |
//will print true, matches "Cat" or "Dog" with "Cat" System.out.println( Pattern.matches("Cat|Dog", "Cat") ); //will print true, matches "Cat" or "Dog" with "Dog" System.out.println( Pattern.matches("Cat|Dog", "Dog") ); //will print true, matches "C" or "F" followed by "at" with "Cat" System.out.println( Pattern.matches("(C|F)at", "Cat") ); //will print true, matches "C" or "F" followed by "at" with "Fat" System.out.println( Pattern.matches("(C|F)at", "Fat") ); |
How to escape metacharacters in the regular expression?
The double backslash (\\) is used to escape the metacharacters in the regular expression to match them as the regular characters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
//will print false, as the | means OR in regex System.out.println( Pattern.matches("hello|world", "hello|world") ); //need to escape the | for literal matching, will print true System.out.println( Pattern.matches("hello\\|world", "hello|world") ); /* * To match a file extension, we need to escape the dot (.) */ //this pattern will consider . means any character, so will return true System.out.println( Pattern.matches("readme.txt", "readmeAtxt") ); //escape the . to make literal matching, will return false System.out.println( Pattern.matches("readme\\.txt", "readmeAtxt") ); //will return true System.out.println( Pattern.matches("readme\\.txt", "readme.txt") ); |
What are the character classes?
We can define a character class using the square brackets “[” and “]”. We can use any of the below given syntaxes to define a character class as per our requirement.
[123] | Matches 1, 2, or 3 |
[^123] | Matches any character except 1, 2, or 3 (not 1, 2, and 3) |
[0-9] | Matches any digit in the range of 0 to 9. |
[a-z] | Matches any character between a to z. |
[a-zA-Z] | Matches any character between a to z and A to Z. |
[a-eF-H] | Matches any character between a to e and F to H. |
[1-3[5-8]] | Matches any digit between 1 to 3 and 5 to 8. Same as above. |
[a-e&&[bc]] | Matches any character between a to e AND b or c so effectively matches only b or c character. |
[a-e&&[^bc]] | Matches any character between a to e except for b and c. |
[a-z&&[^b-e]] | Matches any character between a to z except for characters between b to e. |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
/* * Match individual characters */ //match a, b or c, true System.out.println( Pattern.matches("[abc]", "a") ); //match a, b or c, false System.out.println( Pattern.matches("[abc]", "d") ); /* * Negation of characters */ //match character that is not a,b, or c, true System.out.println( Pattern.matches("[^abc]", "d") ); //match character that is not a,b, or c, false System.out.println( Pattern.matches("[^abc]", "a") ); /* * Match Range of characters */ //match any character between a to e, true System.out.println( Pattern.matches("[a-e]", "a") ); //match any character between a to e, true System.out.println( Pattern.matches("[a-e]", "c") ); //match any character between a to e, true System.out.println( Pattern.matches("[a-e]", "e") ); //match any character between a to e, false System.out.println( Pattern.matches("[a-e]", "f") ); /* * Union of ranges */ //match any digit between 1 to 3 or 5 to 7, true System.out.println( Pattern.matches("[1-35-7]", "1") ); //match any digit between 1 to 3 or 5 to 7, true System.out.println( Pattern.matches("[1-35-7]", "3") ); //match any digit between 1 to 3 or 5 to 7, true System.out.println( Pattern.matches("[1-35-7]", "5") ); //match any digit between 1 to 3 or 5 to 7, true System.out.println( Pattern.matches("[1-35-7]", "7") ); //match any digit between 1 to 3 or 5 to 7, false System.out.println( Pattern.matches("[1-35-7]", "4") ); //match any character between a to c or e to h, true System.out.println( Pattern.matches("[a-c[e-h]]", "a") ); //match any character between a to c or e to h, true System.out.println( Pattern.matches("[a-c[e-h]]", "h") ); //match any character between a to c or e to h, false System.out.println( Pattern.matches("[a-c[e-h]]", "d") ); //match any character between a to c or e to h, false System.out.println( Pattern.matches("[a-c[e-h]]", "i") ); /* * Negation of characters and ranges */ //match any character between a to h, except for e, true System.out.println( Pattern.matches("[a-h&&[^e]]", "a") ); //match any character between a to h, except for e, false System.out.println( Pattern.matches("[a-h&&[^e]]", "e") ); //match any character between a to h, except for characters between e to g, false System.out.println( Pattern.matches("[a-h&&[^e-g]]", "f") ); |
Apart from the user-defined character classes, the regex API provides several predefined character classes for our convenience.
. | Matches any character (does not match the line terminators if DOTALL mode is not enabled) |
\d | Matches any digit. Same as [0-9] |
\D | Matches any non-digit character. Same as [^0-9]. |
\s | Matches any whitespace character. Same as [ \r\t\n\x0B\f] |
\S | Matches any non-whitespace character. Same as [^\s] |
\w | Matches any word character. Same as [a-zA-Z_0-9] i.e. any character between a to z, A to Z, an underscore (_), or 0 to 9 |
\W | Matches any non-word character. Same as [^\w] |
\h | Matches horizontal whitespace character, for example, space or a tab character. Same as [ \t\xA0\u1680\u180e\u2000-\u200a\u202f\u205f\u3000]. |
\H | Matches non-horizontal whitespace character. Same as [^\h] |
\v | Matches vertical whitespace character, for example, a new line character. Same as [\n\x0B\f\r\x85\u2028\u2029] |
\V | Matches non-vertical whitespace character. Same as [^\v] |
We can use them in the same way we used the user-defined character classes above.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
//any digit 1 or 2 times, true System.out.println( Pattern.matches("\\d{1,2}", "12") ); //"hello" followed by zero or more space characters followed by "world", true System.out.println( Pattern.matches("hello\\s*world", "helloworld") ); //true System.out.println( Pattern.matches("hello\\s*world", "hello world") ); //true System.out.println( Pattern.matches("hello\\s*world", "hello world") ); //any word character, true for all System.out.println( Pattern.matches("\\w", "a") ); System.out.println( Pattern.matches("\\w", "Z") ); System.out.println( Pattern.matches("\\w", "9") ); System.out.println( Pattern.matches("\\w", "_") ); //false, not a word character System.out.println( Pattern.matches("\\w", "&") ); |
Apart from these character classes, Java supports below given POSIX character classes (matches only US ASCII).
\p{Lower} | Matches a lower case alphabetic character. Same as [a-z] |
\p{Upper} | Matches an upper case alphabetic character. Same as [A-Z] |
\p{Alpha} | Matches all alphabetic characters. Same as [\p{Upper}\p{Lower}] |
\p{Digit} | Matches a digit. Same as [0-9] |
\p{Alnum} | Matches an alphanumeric character. Same as [\p{Alpha}\p{Digit}] |
\p{Punct} | Matches a punctuation mark. [!”#$%&'()*+,-./:;<=>?@[\]^_`{|}~] |
\p{Graph} | Matches a visible character. Same as [\p{Alnum}\p{Punct}] |
\p{Print} | Matches a printable character. Same as [\p{Graph}x20] |
\p{Cntrl} | Matches a control character. [\x00-\x1F\x7F] |
\p{XDigit} | Matches a hexadecimal digit. [0-9a-fA-F] |
\p{Blank} | Matches a space or a tab. Same as [ \t] |
\p{Space} | Matches a whitespace character [ \t\n\x0B\f\r] |
\p{ASCII} | Matchs all ASCII characters [\x00-\x7F] |
Below given character classes are also supported by Java regular expressions.
\p{javaLowerCase} | Same as java.lang.Character.isLowerCase() |
\p{javaUpperCase} | Same as java.lang.Character.isUpperCase() |
\p{javaWhitespace} | Same as java.lang.Character.isWhitespace() |
\p{javaMirrored} | Same as java.lang.Character.isMirrored() |
How to control the number of times a pattern should match?
We can use the curly brackets {} to control how many times a pattern should match with a given input string.
{x} | The previous character or a pattern should match exactly x number of times. |
{x,} | The previous pattern should match at least x number of times or minimum x number of times. |
{x,y} | The previous character or a pattern should match a minimum of x number of times and the maximum of y number of times. |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
/* * Matches any digit between 0 to 9 exactly one time */ //true System.out.println( Pattern.matches("[0-9]{1}", "1") ); //false as there are two digits System.out.println( Pattern.matches("[0-9]{1}", "12") ); /* * Matches any digit between 0 to 9 minimum one time */ //true System.out.println( Pattern.matches("[0-9]{1,}", "1") ); //true System.out.println( Pattern.matches("[0-9]{1,}", "12") ); //false, no digits matched System.out.println( Pattern.matches("[0-9]{1,}", "a") ); /* * Matches any digit between 0 to 9 minimum one time * and maximum 2 times */ //true System.out.println( Pattern.matches("[0-9]{1,2}", "1") ); //true System.out.println( Pattern.matches("[0-9]{1,2}", "12") ); //false, no digits matched System.out.println( Pattern.matches("[0-9]{1,2}", "a") ); //false, 3 digits found, maximum 2 is allowed System.out.println( Pattern.matches("[0-9]{1,2}", "123") ); |
Tip: You cannot skip the first part before the comma (,) while mentioning the range. If you do, PatternSyntaxException will be thrown. For example, {,4}
will be an invalid pattern. If you just want the specify the maximum number of matches, you can specify the minimum as 0 as given in the below example.
1 2 3 4 5 6 7 8 9 10 |
/* * If you want to match maximum of 4 times, you cannot skip * the minimum part. The below given pattern will throw * PatternSyntaxException. */ //invalid pattern, will throw exception //System.out.println( Pattern.matches("[0-9]{,4}", "1234") ); //instead, specify 0 as minimum and 4 as maximum System.out.println( Pattern.matches("[0-9]{0,4}", "1234") ); |
Let’s take a break and test the knowledge
Now we have got a basic understanding of how the regular expression works. Let’s create a complex pattern using the knowledge we have gained until now. The example I want to cover is to validate date syntax in dd-mm-yyyy format.
The first part of the pattern is dd i.e. the day. The day of the month should be two digits and between 1 to 31. Breaking it up will give us the days from 01 to 09, 10 to 19, 20 to 29, 30, and 31.
So the pattern will be “0[1-9]|[12][0-9]|3[01]”. The whole pattern means 0 followed by any digits between 1 to 9 OR 1 or 2 followed by any digits between 0 to 9 OR 3 followed by either 0 or 1. It should cover all the days between 1 to 31.
Let’s test this part.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
//false, single digit System.out.println( Pattern.matches("0[1-9]|[12][0-9]|3[01]", "0") ); //false, double digits but not in range System.out.println( Pattern.matches("0[1-9]|[12][0-9]|3[01]", "00") ); //false, single digit System.out.println( Pattern.matches("0[1-9]|[12][0-9]|3[01]", "1") ); //true System.out.println( Pattern.matches("0[1-9]|[12][0-9]|3[01]", "01") ); //true System.out.println( Pattern.matches("o[1-9]|[12][0-9]|3[01]", "31") ); //true System.out.println( Pattern.matches("0[1-9]|[12][0-9]|3[01]", "21") ); //true System.out.println( Pattern.matches("0[1-9]|[12][0-9]|3[01]", "11") ); //false, double digits but out of range System.out.println( Pattern.matches("0[1-9]|[12][0-9]|3[01]", "32") ); |
The second part of the pattern is the month part, which should be two digits and must be between 1 to 12. So the pattern will be “0[1-9]|1[012]” means 0 followed by any digit between 1 to 9 (to cover months between 1 to 9) OR 1 followed by 0, 1 or 2 (to cover months between 10 to 12). Let’s test it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
//false, single digit, out of range System.out.println( Pattern.matches("0[1-9]|1[012]", "0") ); //false, single digit System.out.println( Pattern.matches("0[1-9]|1[012]", "1") ); //true System.out.println( Pattern.matches("0[1-9]|1[012]", "01") ); //true System.out.println( Pattern.matches("0[1-9]|1[012]", "10") ); //true System.out.println( Pattern.matches("0[1-9]|1[012]", "12") ); //false, out of range System.out.println( Pattern.matches("0[1-9]|1[012]", "13") ); |
Now let’s see the year part. The year could be anything but it must be exactly 4 digits long which is fairly easy to convert to a pattern. The pattern to check the year will be “[0-9]{4}” and it means any digit between 0 to 9 four times.
Let’s test it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
//false, single digit System.out.println( Pattern.matches("[0-9]{4}", "0") ); //false, not 4 digits System.out.println( Pattern.matches("[0-9]{4}", "000") ); //true System.out.println( Pattern.matches("[0-9]{4}", "0000") ); //true System.out.println( Pattern.matches("[0-9]{4}", "0578") ); //true System.out.println( Pattern.matches("[0-9]{4}", "2020") ); //false, 5 digits System.out.println( Pattern.matches("[0-9]{4}", "10000") ); |
Now let’s put them all together in one pattern to validate date syntax in the “dd-mm-yyyy” format. The whole pattern will be like “(0[1-9]|[12][0-9]|3[01])-(0[1-9]|1[012])-([0-9]{4})”. We just clubbed all the individual parts using groups and put a “-” between them.
Let’s try out the whole pattern against the example dates.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
//false, invalid day System.out.println( Pattern.matches("(0[1-9]|[12][0-9]|3[01])-(0[1-9]|1[012])-([0-9]{4})", "1-12-2003") ); //false, invalid month System.out.println( Pattern.matches("(0[1-9]|[12][0-9]|3[01])-(0[1-9]|1[012])-([0-9]{4})", "01-1-2003") ); //false, invalid year System.out.println( Pattern.matches("(0[1-9]|[12][0-9]|3[01])-(0[1-9]|1[012])-([0-9]{4})", "01-01-100") ); //false, invalid month System.out.println( Pattern.matches("(0[1-9]|[12][0-9]|3[01])-(0[1-9]|1[012])-([0-9]{4})", "12-31-2003") ); //false, invalid month System.out.println( Pattern.matches("(0[1-9]|[12][0-9]|3[01])-(0[1-9]|1[012])-([0-9]{4})", "12-23-2020") ); //false, invalid day System.out.println( Pattern.matches("(0[1-9]|[12][0-9]|3[01])-(0[1-9]|1[012])-([0-9]{4})", "32-12-2020") ); //false, invalid month System.out.println( Pattern.matches("(0[1-9]|[12][0-9]|3[01])-(0[1-9]|1[012])-([0-9]{4})", "13-00-2020") ); //true, valid date syntax System.out.println( Pattern.matches("(0[1-9]|[12][0-9]|3[01])-(0[1-9]|1[012])-([0-9]{4})", "01-01-1002") ); //true, valid date syntax System.out.println( Pattern.matches("(0[1-9]|[12][0-9]|3[01])-(0[1-9]|1[012])-([0-9]{4})", "01-01-2020") ); |
The above example is just meant to understand how to create a pattern for your requirements. Please note that the regex can only validate the date syntax, not the actual date. For example, our pattern will say valid date to “31-02-2004” while it is not (because February does not have 31 days). Please visit how to validate a date example to understand the right way.
How to match the boundaries using the regular expression?
The Java regular expressions support below given boundary matchers.
\b | Matches with the word boundary |
\B | Matches with the non-word boundary |
^ | In single-line mode, it matches at the start of the input. In the multiline mode, it matches at the start of each line. |
\A | Matches at the start of the input. Regardless of a single line or multiline mode, it matches exactly once, i.e. at the start of the input. |
$ | In single-line mode, it matches at the end of the input. In the multiline mode, it matches at the end of each line. |
\Z | Matches at the end of the input or the last input terminator if present regardless of the line mode. |
\z | Matches at the end of the input. Regardless of a single line or multiline mode, it matches exactly once, i.e. at the end of the input. |
\G | Matches at the end of the previous match |
The boundary matchers are very useful to indicate the position where we want to match in the input string.
1 2 3 4 5 6 7 8 9 10 11 |
//one or more digits at the start of the input, prints true System.out.println( Pattern.matches("^\\d+", "123") ); //one or more digits at the start of the input, prints false System.out.println( Pattern.matches("^\\d+", "a123") ); //one or more digits at the end of the input, prints true System.out.println( Pattern.matches(".*\\d+$", "a123") ); //one or more digits at the end of the input, prints false System.out.println( Pattern.matches(".*\\d+$", "a123b") ); |
Output
1 2 3 4 |
true false true false |
The below given code matches the word “dog” with the input string.
1 2 3 4 5 6 7 8 9 |
Pattern pattern = Pattern.compile("dog"); Matcher matcher = pattern.matcher("A dog likes eating hotdog"); while(matcher.find()){ System.out.println( "Match \"" + matcher.group() + "\"" + " starting from index " + matcher.start() + " to index " + (matcher.end() - 1)); } |
Output
1 2 |
Match "dog" starting from index 2 to index 4 Match "dog" starting from index 22 to index 24 |
The pattern “dog” not only matched with the word “dog” in the sentence but it also matched the string “dog” within the “hotdog”. If you want to match the whole word only, you can use the word boundary as given below.
1 2 3 4 5 6 7 8 9 |
Pattern pattern = Pattern.compile("\\bdog\\b"); Matcher matcher = pattern.matcher("A dog likes eating hotdog"); while(matcher.find()){ System.out.println( "Match \"" + matcher.group() + "\"" + " starting from index " + matcher.start() + " to index " + (matcher.end() - 1)); } |
Output
1 |
Match "dog" starting from index 2 to index 4 |
The pattern “\\bdog\\b” means a word boundary, followed by characters “dog”, followed by a word boundary, so it matched with a whole word only.
What are capturing groups and how to use them in regular expressions?
The group in the regular expression is a single unit of characters and created using the opening and closing parentheses “(” and “)”. The part of the input string captured using the group is stored for the later reference.
Groups are extremely useful when we want to extract some information from the input text.
The below given example extracts the number from the string using the pattern “(\\d+)” which means a group of one or more digits.
1 2 3 4 5 6 |
Pattern pattern = Pattern.compile("(\\d+)"); Matcher matcher = pattern.matcher("This is 100% pure Java"); if(matcher.find()){ System.out.println( matcher.group() ); } |
Output
1 |
100 |
We can have more than one group in our regex including the nested groups. The groupCount
method of the Matcher class returns the total number of groups in the pattern.
The capturing groups are numbered by counting the opening parenthesis from the left to right. For example, the expression (x(y(z)) contains below given 3 groups in the given sequence.
- (x(y(z))
- (y(z))
- (z)
There is also a special group, group 0, that represents the entire expression and is not included in the total number of groups obtained using the groupCount
method. We can get the matched group by specifying the group number in the group
method of the Matcher class.
1 |
public String group(int group) |
It returns the input string captured by the given group number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
Pattern pattern = Pattern.compile("I got (\\d+)% in (\\d+) semester"); Matcher matcher = pattern.matcher("I got 100% in 4 semester"); if(matcher.matches()){ //the group 0 is entire expression System.out.println( matcher.group(0) ); //this is same as group(0) System.out.println( matcher.group() ); //this is group 1 i.e. 100 System.out.println( matcher.group(1) ); //this is group 2 i.e. 4 System.out.println( matcher.group(2) ); } |
Output
1 2 3 4 |
I got 100% in 4 semester I got 100% in 4 semester 100 4 |
We can also get the starting and ending index of the individual groups using the start
and end
methods of the Matcher class respectively.
1 2 3 4 5 6 7 8 9 |
Pattern pattern = Pattern.compile("(\\d+)"); Matcher matcher = pattern.matcher("I got 100% in 4 semester"); while(matcher.find()){ System.out.println( "Match \"" + matcher.group() + "\"" + " starting from index " + matcher.start() + " to index " + (matcher.end() - 1) ); } |
Output
1 2 |
Match "100" starting from index 6 to index 8 Match "4" starting from index 14 to index 14 |
The captured group is stored for the later reference which we can refer to using the “\” followed by the group number.
1 2 3 4 5 6 7 8 9 |
Pattern pattern = Pattern.compile("(ab)\\1"); Matcher matcher = pattern.matcher("abab1234abab"); while(matcher.find()){ System.out.println( "Match \"" + matcher.group() + "\"" + " starting from index " + matcher.start() + " to index " + (matcher.end() - 1)); } |
Output
1 2 |
Match "abab" starting from index 0 to index 3 Match "abab" starting from index 8 to index 11 |
The above example captures the group of character “a” followed by “b” and then later it is referenced using the group number 1 in the regular expression pattern. The pattern “(ab)\\1” is equivalent to the pattern “abab”.
Additionally, you can refer to these groups while replacing the values in the string using the “$” sign followed by the group number as given below.
1 |
System.out.println( "abab1234abab".replaceAll("(ab)", "$1c") ); |
Output
1 |
abcabc1234abcabc |
The above given code finds the group of characters “a” followed by “b” in the input string “abab1234abab”. It then replaces the group using the $1 followed by character “c”. Effectively, it replaces all matches of “ab” with “abc”.
What are the quantifiers and how to use them in regular expressions?
The Java RegEx API provides three types of quantifiers namely greedy, reluctant, and possessive as given below. The default quantifier is greedy.
Greedy | Reluctant | Possessive | |
* | *? | *+ | Matches 0 or more times |
+ | +? | ++ | Matches one or more times |
? | ?? | ?+ | Matches 0 or one time |
{x} | {x}? | {x}+ | Matches exactly x times |
{x, } | {x, }? | {x, }+ | Matches minimum x times |
{x,y} | {x,y}? | {x,y}+ | Matches minimum x times and maximum y times |
At the first look, they all look the same, for example, “*”, “*?”, and “*+” all three matches 0 or more times. But there is a difference between them.
The greedy quantifier consumes or reads the whole input string first even before attempting to match. If the match is not found, the regex engine backtracks the input string by one character at a time to match until the match is found or there are no more characters to backtrack from. It tries to make the longest match possible.
The reluctant quantifier is exactly the opposite of the greedy quantifier. It tries the shortest match possible and starts to match at the beginning of the input string. If the match is not found, it consumes one character at a time until the match is found or it has consumed all the characters of the input string.
The possessive quantifier is similar to the greedy quantifier as it also consumes the whole input first. But unlike the greedy quantifier, the possessive quantifier does not backtrack the characters if the match is not found. It eats the entire input string and tries to match only once.
Let’s see an example of what all this means. The below given example tries to match any character zero or more times followed by 12 against an input string.
1 2 3 4 5 6 7 8 9 |
Pattern pattern = Pattern.compile(".*12"); Matcher matcher = pattern.matcher("1234512"); while(matcher.find()){ System.out.println( "Match \"" + matcher.group() + "\"" + " starting from index " + matcher.start() + " to index " + (matcher.end() - 1)); } |
Output
1 |
Match "1234512" starting from index 0 to index 6 |
The greedy “.*” quantifier consumes the whole input string “1234512” first but it fails to match with the overall expression because there are no characters left to match with the last 12. So the regex engine backtracks one character from the input string “123451” to match against .* but it still fails to match with the whole expression.
So it backtracks one more character and tries to match the “.*” with “12345” with the overall pattern. At this point, the match is found. So it tries the longest match first.
Now let’s see what happens when we use the pattern with reluctant *? to match against the same input string.
1 2 3 4 5 6 7 8 9 |
Pattern pattern = Pattern.compile(".*?12"); Matcher matcher = pattern.matcher("1234512"); while(matcher.find()){ System.out.println( "Match \"" + matcher.group() + "\"" + " starting from index " + matcher.start() + " to index " + (matcher.end() - 1)); } |
Output
1 2 |
Match "12" starting from index 0 to index 1 Match "34512" starting from index 2 to index 6 |
In this case, the engine starts matching .*? (any character zero or more times followed by 12) with as few characters as possible and starts matching at the start of the input string. Since the match is not found, it consumes one character from the string and matches .*? with “1” but it still fails. So it consumes one more character and matches .*? with “12”. At this point, the first match is found.
Similarly, the second match is also found at the end of the string. So, basically it starts matching at the start of the string and tries to match as few characters as possible.
Now let’s try the possessive quantifier .*+ with the same input string and pattern.
1 2 3 4 5 6 7 8 9 |
Pattern pattern = Pattern.compile(".*+12"); Matcher matcher = pattern.matcher("1234512"); while(matcher.find()){ System.out.println( "Match \"" + matcher.group() + "\"" + " starting from index " + matcher.start() + " to index " + (matcher.end() - 1)); } |
Output
1 |
It could not find any match. The possessive quantifier first consumes the whole string to match against .*+ and tries to match against the overall pattern. Since there are no more characters left in the input string to match with the ending 12, the match fails.
Unlike the greedy quantifier, the possessive quantifier does not backtrack the characters to match, so it tries to match only once with the entire input string.
You may ask why do we have the possessive quantifier then? Well, it offers better performance as compared to greedy quantifiers in the cases where we need to match against the entire input. Since it does not backtrack, fewer matches need to be done.
What are the negative and positive lookahead and lookbehind expressions?
The lookahead and lookbehind expressions are collectively referred to as lookaround expressions. As the names suggest, they look around left or right from the current position. The lookaround expression matches the character sequence without actually including them in the match. It does not consume the input sequence, it just tests whether the given pattern can match or not.
For example, if we want to search a digit “9” followed digit “1”, we will use something like this using the character class.
1 2 3 4 5 6 7 8 9 |
Pattern pattern = Pattern.compile("9[1]"); Matcher matcher = pattern.matcher("9111"); while(matcher.find()){ System.out.println( "Match \"" + matcher.group() + "\"" + " starting from index " + matcher.start() + " to index " + (matcher.end() - 1)); } |
Output
1 |
Match "91" starting from index 0 to index 1 |
We wanted to match the digit “9” that is followed by “1”. But what we got is “91” not “9”. This is where the lookaround expression comes handy. It does not consume the “1” but only checks if we have a digit “9” in the input string that is followed by “1” without consuming the digit “1”.
Positive lookahead expression
The positive look ahead expression is used when we want to find something that is immediately followed by something else and it is created using "(?=RegEx)"
syntax. I will use the same example given above to find a digit “9” that is followed by “1”. This expression is successful only when the given regex matches.
1 2 3 4 5 6 7 8 9 |
Pattern pattern = Pattern.compile("9(?=1)"); Matcher matcher = pattern.matcher("9111"); while(matcher.find()){ System.out.println( "Match \"" + matcher.group() + "\"" + " starting from index " + matcher.start() + " to index " + (matcher.end() - 1)); } |
Output
1 |
Match "9" starting from index 0 to index 0 |
So now we got only 9. It just tested that there is a 9 at index 0 that is followed by the digit “1” at index 1 but did not consume the digit “1” in the match.
Negative lookahead expression
It is similar to the positive lookahead expression but it is used when we want something that is not immediately followed by something else. The negative lookahead expression is created using the "(?!RegEx)"
syntax. This expression is successful only when the given regex fails to match.
For example, the below given expression finds the digit “9” that is not followed by digit “1”.
1 2 3 4 5 6 7 8 9 |
Pattern pattern = Pattern.compile("9(?!1)"); Matcher matcher = pattern.matcher("9192"); while(matcher.find()){ System.out.println( "Match \"" + matcher.group() + "\"" + " starting from index " + matcher.start() + " to index " + (matcher.end() - 1)); } |
Output
1 |
Match "9" starting from index 2 to index 2 |
The first “9” at index 0 did not match because it is followed by digit “1” while the second “9” at index 2 of the input string did as it is not followed by the digit “1” but “2”.
Positive lookbehind expression
The positive lookbehind expression looks in a backward direction. It is used when we want something that is preceded by something else or in other words to check if something else comes before something in the input string.
The positive lookbehind expression is created using the “(?<=RegEx)
” syntax.
The below given example tries to match a digit “1” that is preceded by a digit “9” in the input string (or in other words, we want to find a digit “1” having “9” as the previous character). Keep in mind that we do not want “91” as a match, we just want “1” that is preceded by a digit “9”.
1 2 3 4 5 6 7 8 9 |
Pattern pattern = Pattern.compile("(?<=9)1"); Matcher matcher = pattern.matcher("9111"); while(matcher.find()){ System.out.println( "Match \"" + matcher.group() + "\"" + " starting from index " + matcher.start() + " to index " + (matcher.end() - 1)); } |
Output
1 |
Match "1" starting from index 1 to index 1 |
The digit “1” at the index 1 is matched because the previous digit is “9” at index 0. However, the digit “1” at index 2 did not match because it did not have the digit “9” before it.
Negative lookbehind expression
The negative lookbehind expression is similar to the positive lookbehind expression but it is used to check whether something is not preceded by something else. It is created using the "(?<!RegEx)"
syntax.
The below given example tries to match a digit “1” that is not preceded by the digit “9” (in other words a digit “1” whose previous digit is not “9”).
1 2 3 4 5 6 7 8 9 |
Pattern pattern = Pattern.compile("(?<!9)1"); Matcher matcher = pattern.matcher("9119"); while(matcher.find()){ System.out.println( "Match \"" + matcher.group() + "\"" + " starting from index " + matcher.start() + " to index " + (matcher.end() - 1)); } |
Output
1 |
Match "1" starting from index 2 to index 2 |
Here the first “1” at index 1 did not match because its previous digit was “9”. However, the digit “1” at index 2 matched because the previous digit of it was not “9”.
Below given are some of the additional Java Regular Expression examples that show the real-world usage of the regular expressions in Java.
Java Regular Expression Examples (RegEx examples)
String Operations
- How to allow only alpha-numeric characters (only letters and digits) in String using regular expression\
- How to keep only numbers in a string using regular expression
- How to keep only letters in a string using regular expression
- How to keep only letters and numbers in a string using regular expression
- How to replace a substring in a string using the replaceAll method and regular expression
- How to count the number of a substring in a string using regular expression
- How to remove non-ASCII characters from a string using regular expression
- How to remove leading zeros from the string using regular expression (zeros at the front)
- How to remove HTML tags from a string using regular expression
- How to check if the string contains a number using regular expression
- How to check if the string starts with another string using regular expression
- How to check if the string starts with a number using regular expression
- How to check if the string ends with another string using regular expression
- How to create case insensitive regular expression in Java
- How to count the number of words in a string using regular expression
- How to convert List to String using regular expression
- How to convert a string to string array using regular expression
- How to convert a string to an ArrayList using regular expression
- How to convert comma-separated string to ArrayList using regular expression
- How to convert an int array to string using regular expression
- How to remove leading and trailing spaces from string using regular expression (spaces at the start and end)
- How to remove multiple spaces from string using regular expression (spaces between words)
- How to split a string using regular expression in Java
- How to split a string into the equal length substrings using regular expression
- How to split a string by words using regular expression
- How to split a string by dot using regular expression
- How to split a string by pipe using regular expression
- How to split a string by comma using regular expression
- How to split a string by new lines using regular expression
- How to use positive lookahead and negative lookahead in Java regular expressions
- How to use positive lookbehind and negative lookbehind expression in Java regex
- What is a java.util.regex.PatternSyntaxException: Unclosed group near index 1 exception and how to fix it
- What is a java.util.regex.PatternSyntaxException: Unclosed character class near index 0 exception and how to fix it
- What is a java.util.regex.PatternSyntaxException: Dangling meta character near index 0 exception and how to fix it
- Java Regex count matches example
- How to match a word boundary in Java regex
- How to check special characters in a string using regex in Java
- How to get the substring match index and last index using Java regex (indexOf, lastIndexOf)
- How to match groups in Java regex
- What is a Java.lang.IndexOutOfBoundsException: No group 1 exception and how to fix it
- How to use OR condition in Java regex
- How to match at the beginning and at the end using Java regex
- How to extract a date from a string using Java regex
- What is a java.util.regex.PatternSyntaxException: Illegal repetition exception and how to fix it
- How to mask phone numbers using Java RegEx
- How to extract numbers from string using Java regex
- How to extract text between double quotes and single quotes using Java Regex
- How to match at least one uppercase, lowercase, or special character using Java regex
- How to remove all special characters from a string using Java regex
- How to escape dot and match in Java regex
- How to escape brackets and match in regex
- How to extract string between brackets using Java regex
Validations
- How to validate username using regular expression in Java
- How to validate password using regular expression in Java
- How to check password strength using regular expression Java
- How to validate a date using regular expressions in Java
- How to validate 10 digit mobile number using regex in Java
- How to validate dd/mm/yyyy and mm/dd/yyyy dates using regex in Java
- How to validate PAN card number using regex in Java
- How to validate Aadhar card number using regex in Java
- How to validate a file name and file extension using Java regex
- How to validate Indian Pincode using Java regex
- How to validate US ZIP code using Java RegEx
- How to validate a MAC address using Java regex
References:
Java 8 Pattern Class Documentation
Java 8 Matcher Class Documentation
Please let me know if you liked the Java Regular Expression tutorial with examples in the comments section below.