Java RegEx – indexOf & lastIndexOf substring example shows how to find an index of a substring in a string using regular expression pattern instead of indexOf or lastIndexOf methods of the String class.
How to find the index of a substring using regex in Java?
The Java String class provides overloaded indexOf
methods to find the index of a specific substring or subsequence within given string content. These indexOf
methods are overloaded for String as well char data types.
However, many times some use case requires us to search for a complicated pattern inside of the string content instead of a simple substring. The indexOf
method of the String class does not accept a regex pattern. For example, you want to find an index of a number in the string, but you do not know the exact number you are looking for. It could be 1, or 100, or 1000. In this case, the indexOf
method of the String class cannot be used.
In that case, we can use the Pattern and Matcher class. Using the find
method and start
method of the Matcher class, we can implement our own indexOf method that accepts a regex pattern 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 |
package com.javacodeexamples.regex; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegExIndexOfExample { public static void main(String[] args) { //string content String strSource = "Processing file 51 of 100"; //we want to find index of a number 51 String strPattern = "(\\d+)"; System.out.println( indexOfRegEx(strSource, strPattern) ); } //returns -1 in case the pattern is not found in the string private static int indexOfRegEx(String strSource, String strRegExPattern) { int idx = -1; //compile pattern from string Pattern p = Pattern.compile(strRegExPattern); //create a matcher object Matcher m = p.matcher(strSource); //if pattern is found in the source string if(m.find()) { //get the start index using start method of the Matcher class idx = m.start(); } return idx; } } |
Output
1 |
16 |
As you can see from the output, our indexOf method found the number in a string at index 19. The same code can be written in a very compact form as given below.
1 2 3 4 |
private static int indexOfRegEx(String strSoruce, String strRegExPattern) { Matcher m = Pattern.compile(strRegExPattern).matcher(strSoruce); return m.find() ? m.start() : -1; } |
Important Note:
The indexOf
methods of the String class are more efficient in terms of performance as compared to our version of the method using the regex pattern. For normal substring matches, using them over the pattern version is recommended.
How to find the last index of a substring using regex in Java?
Finding the last index of a substring is a little bit tricky as compared to the first index. One way is to use a while loop to iterate through all the matches and then use the last match to get the last index 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 |
package com.javacodeexamples.regex; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegExIndexOfExample { public static void main(String[] args) { //string content String strSource = "Processing file 51 of 100"; //we want to find index of a number 51 String strPattern = "(\\d+)"; System.out.println( lastIndexOfRegEx(strSource, strPattern) ); } //returns -1 in case the pattern is not found in the string private static int lastIndexOfRegEx(String strSource, String strRegExPattern) { int idx = -1; //compile pattern from string Pattern p = Pattern.compile(strRegExPattern); //create a matcher object Matcher m = p.matcher(strSource); //loop till we get more matches while(m.find()) { /* * this will hold the start index * of the last match index when * the loop completes */ idx = m.start(); } return idx; } } |
Output
1 |
22 |
As you can see from the output, this time our code found the index of the last number i.e. number 100 in string content.
If you want to learn more about regex in Java, visit regex tutorial.
Please let me know your views in the comments section below.