这个 Java 程序在尝试调用 group(1)
时向我显示 IndexOutOfBoundsException。如果我用 0
替换 1
,那么整行都会被打印出来。我该怎么办?
Pattern pattern = Pattern.compile("<abhi> abhinesh </abhi>");
Matcher matcher = pattern.matcher("<abhi> abhinesh </abhi>");
if (matcher.find())
System.out.println(matcher.group(1));
else
System.out.println("Not found");
最佳答案
索引从 0 开始,因此使用 matcher.group(0)
编辑:要匹配标记之间的文本,请使用此正则表达式 <abhi>(.*)<\\/abhi>
关于java - 使用 Matcher.find() 时出现 IndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24087556/