I’m attempting to find the indices of multiple matches in a String using Regex (test code below), for use with external libraries.
static String content = "a {non} b {1} c {1}";
static String inline = "\\{[0-9]\\}";
public static void getMatchIndices()
{
Pattern pattern = Pattern.compile(inline);
Matcher matcher = pattern.matcher(content)
while (matcher.find())
{
System.out.println(matcher.group());
Integer i = content.indexOf(matcher.group());
System.out.println(i);
}
}
OUTPUT:
{1}
10
{1}
10
It finds both groups, but returns an index of 10 for both. Any ideas?
You can do this with regexp. The following will find the locations in the string.