EN
Java - get index of pattern in string using regex
10 points
In java it is possible to get text index of pattern in string using regex in following way.
xxxxxxxxxx
1
String regex = "bc";
2
String text = "aabbcc";
3
4
Pattern pattern = Pattern.compile(regex);
5
Matcher matcher = pattern.matcher(text);
6
7
if (matcher.find()) {
8
System.out.println("Start index: " + matcher.start());
9
System.out.println("End index: " + matcher.end());
10
System.out.println("Found text: " + matcher.group());
11
}
Output:
xxxxxxxxxx
1
Start index: 3
2
End index: 5
3
Found text: bc