0
這是All overlapping substrings matching a java regex的後續操作。高效查找正則表達式的所有重疊匹配項
有沒有辦法讓這段代碼更快?
public static void allMatches(String text, String regex)
{
for (int i = 0; i < text.length(); ++i) {
for (int j = i + 1; j <= text.length(); ++j) {
String positionSpecificPattern = "((?<=^.{"+i+"})("+regex+")(?=.{"+(text.length() - j)+"}$))";
Matcher m = Pattern.compile(positionSpecificPattern).matcher(text);
if (m.find())
{
System.out.println("Match found: \"" + (m.group()) + "\" at position [" + i + ", " + j + ")");
}
}
}
}
真棒,非常感謝!是的,我認爲添加'^'和'$'錨有助於。事實證明,爲了我的目的,我還需要'useTransparentBounds(true)'。甜! – dsg 2012-07-07 06:15:51