我正在尝试匹配JTextArea中的选择,我使用Matcher.region()来定义匹配的边界:
JTextComponent t;
Pattern p = Pattern.compile("string", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
Matcher m = p.matcher(t.getText()).region(t.getSelectionStart(), t.getSelectionEnd());
m.useAnchoringBounds(false);
if(m.find()) {
System.out.println("Found match from " + m.start() + " to " + m.end());
}
else {
System.out.println("No match found");
}
以上按预期工作,它会在区域内找到第一个匹配项 - 如果找不到匹配项,则找不到匹配项。
但是,我试图循环遍历区域内的匹配(搜索和替换类型函数),如果我在区域内指定find()的起始位置,那么它匹配区域之外:
int cPos = m.regionStart();
if (m.find(cPos) || m.find(m.regionStart())) {
System.out.println("Found match from " + m.start() + " to " + m.end());
cPos = m.end();
}
else {
System.out.println("No match found");
}
这是一个错误,还是我在指定起始位置时打破该区域 - 即使它包含在该区域内?
谢谢, 克里斯