Я пытаюсь получить индексы для каждого шаблона, который я нахожу в документе. Пока что у меня есть: < /p>
Код: Выделить всё
String temp = "This is a test to see HelloWorld in a test that sees HelloWorld in a test";
Pattern pattern = Pattern.compile("HelloWorld");
Matcher matcher = pattern.matcher(temp);
int current = 0;
int start;
int end;
while (matcher.find()) {
start = matcher.start(current);
end = matcher.end(current);
System.out.println(temp.substring(start, end));
current++;
}
По какой -то причине
продолжает находить только первый случай Helloworld в Temp , который приводит к бесконечному циклу. Честно говоря, я не был уверен, можно ли использовать Matcher.start (current) и matcher.end (current) - это было просто дикое предположение, потому что Matcher.group (current) работал раньше. На этот раз мне нужны фактические индексы, хотя так же, что matcher.group () не сработает для меня.
Подробнее здесь:
https://stackoverflow.com/questions/142 ... ch-indexes