我正在尝试使用正则表达式在字符串中查找多个匹配项的索引(请参考以下测试代码),以便与外部库一起使用。
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);
}
}
输出:
{1}
10
{1}
10
它找到了两个组,但对于这两个组都返回了索引10。有什么想法吗?