2012-07-07 25 views
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 + ")"); 
     } 
     } 
    } 
    } 

回答

1

在其他問題,你提到的匹配器region()方法,但你沒有充分利用它。是什麼讓它非常有價值的是,錨點將在該區域的邊界處匹配,就好像它們是獨立字符串的邊界一樣。假設你已經設置了useAnchoringBounds()選項,但這是默認設置。

public static void allMatches(String text, String regex) 
{ 
    Matcher m = Pattern.compile(regex).matcher(text); 
    int end = text.length(); 
    for (int i = 0; i < end; ++i) 
    { 
    for (int j = i + 1; j <= end; ++j) 
    { 
     m.region(i, j); 

     if (m.find()) 
     { 
     System.out.printf("Match found: \"%s\" at position [%d, %d)%n", 
          m.group(), i, j); 
     } 
    } 
    } 
} 

鑑於您的樣本字符串和正則表達式:

allMatches("String t = 04/31 412-555-1235;", "^\\d\\d+$"); 

...我得到這樣的輸出:

Match found: "04" at position [11, 13) 
Match found: "31" at position [14, 16) 
Match found: "41" at position [17, 19) 
Match found: "412" at position [17, 20) 
Match found: "12" at position [18, 20) 
Match found: "55" at position [21, 23) 
Match found: "555" at position [21, 24) 
Match found: "55" at position [22, 24) 
Match found: "12" at position [25, 27) 
Match found: "123" at position [25, 28) 
Match found: "1235" at position [25, 29) 
Match found: "23" at position [26, 28) 
Match found: "235" at position [26, 29) 
Match found: "35" at position [27, 29) 
+0

真棒,非常感謝!是的,我認爲添加'^'和'$'錨有助於。事實證明,爲了我的目的,我還需要'useTransparentBounds(true)'。甜! – dsg 2012-07-07 06:15:51

相關問題