2014-01-28 21 views
0

我有以下字符串:Java的替代確切的子字符串從

String str = "6,20,9,10,19,11,5,3,1,2"; 

,我想更換確切字符串,21,一個空字符串,避免了串,2011,被捲入替代。

我有這樣的代碼:

// assuming that substr = ",2" or "1," 

if(str.startsWith(substr+",")){ 
    str = str.replace("^"+substr+",$",""); 
}    
else if(str.contains(","+substr+",")){ 
    str = str.replace("^"+substr+",$",""); 
} 
else if(str.endsWith(","+substr)){ 
    str = str.replace("^,"+substr+"$",""); 
} 

但似乎我錯了,用正則表達式。我應該使用哪種正則表達式來解決它?

回答

2

我的回答是:「你想要達到什麼目的?」

如果你想過濾我會去String.split(","),然後篩選字符串數組中生成的項目。最終我會加入他們(如果你不受運行時限制)。

喜歡的東西(未經測試):

String[] splits = myString.split(","); 
// ... manipulate splits (filter into a list if you want...) 
StringUtils.join(splits, ", "); // possibly replace with array from filtered list 
  • 拆分可以通過過濾列表的toArray

替代解決你的方式可能會非常棘手。當你嘗試匹配一件物品時,你會消除與鄰居相匹配的機會。你需要做的是匹配聚合。

這僅僅是一個開始(未經測試,基於this):

List<String> matches = new ArrayList<String>(); 
    Matcher m = Pattern.compile("(^[12],|,[12],|[12]$)").matcher("1,6,20,9,10,19,11,5,3,1,2"); 
    if (m.find()) { 
     do { 
      System.out.println("Matched "+m.group()); 
      matches.add(m.group()); 
     } while (m.find(m.start()+1)); 
    } 

    for (String match : matches){ 
     System.out.println(match); 
    } 

你可以嘗試和配置文件運行時添加這兩種方法,也許以後擴大:-)我的回答

+0

但在這種情況下,我必須做一個循環找元素,將其刪除,最後加入的所有元素重新打造的字符串。 。我認爲replace()方法更快或者沒有? –

+0

取決於你想要做什麼。字符串操作對於運行時並不是很好,所以我明確表示只有在不受約束的情況下才是解決方案。如果你可以詳細說明你的約束條件,我相信可以找到更好的解決方案。 –

+0

我問實際上是否有一個特定的正則表達式來幫助我在這種情況下,或者如果我錯了我貼的代碼.. –

0

我認爲問題是「如何從列表中刪除一個值」,但只有值(例如2而不是12,13不是413),並且縮小列表,即刪除前面或後面的逗號,如果有的話,但不是兩者。

答案很簡單:

String x = Pattern.quote(textToRemove); 
Pattern p = Pattern.compile("^"+x+"$|^"+x+",|,"+x+"$|,"+x+"(?=,)"); 
String output = p.matcher(input).replaceAll(""); // or replaceFirst 

例子:

Input:  "6,20,9,10,19,101,5,3,1,2" 
Remove "2": "6,20,9,10,19,101,5,3,1" -- last value, don't remove 20 
Remove "20": "6,9,10,19,101,5,3,1,2" -- middle value 
Remove "1": "6,20,9,10,19,101,5,3,2" -- don't remove 10, 19, or 101 
Remove "10": "6,20,9,19,101,5,3,1,2" -- don't remove 101 
Remove "6": "20,9,10,19,101,5,3,1,2" -- first value 
Remove "77": "6,20,9,10,19,101,5,3,1,2" -- nothing removed 

Input:  "6" 
Remove "6": ""       -- only value 

代碼:

private static void test(String input, String textToRemove) { 
    String rmv = Pattern.quote(textToRemove); 
    Pattern p = Pattern.compile("^" + rmv + "$" +  // matches only value 
           "|^" + rmv + "," +  // matches first value + ',' 
           "|," + rmv + "$" +  // matches ',' + last value 
           "|," + rmv + "(?=,)"); // matches ',' + middle value (+ ',') 
    String output = p.matcher(input).replaceAll(""); // or replaceFirst 
    System.out.printf("Remove %-4s from %-26s: %s%n", 
         '"' + textToRemove + '"', 
         '"' + input + '"', 
         '"' + output + '"'); 
} 

測試:

public static void main(String[] args) throws Exception { 
    // 
    test("6,20,9,10,19,101,5,3,1,2", "2"); 
    test("6,20,9,10,19,101,5,3,1,2", "20"); 
    test("6,20,9,10,19,101,5,3,1,2", "1"); 
    test("6,20,9,10,19,101,5,3,1,2", "10"); 
    test("6,20,9,10,19,101,5,3,1,2", "6"); 
    test("6,20,9,10,19,101,5,3,1,2", "77"); 
    test("6"      , "6"); 
} 

輸出:

Remove "2" from "6,20,9,10,19,101,5,3,1,2": "6,20,9,10,19,101,5,3,1" 
Remove "20" from "6,20,9,10,19,101,5,3,1,2": "6,9,10,19,101,5,3,1,2" 
Remove "1" from "6,20,9,10,19,101,5,3,1,2": "6,20,9,10,19,101,5,3,2" 
Remove "10" from "6,20,9,10,19,101,5,3,1,2": "6,20,9,19,101,5,3,1,2" 
Remove "6" from "6,20,9,10,19,101,5,3,1,2": "20,9,10,19,101,5,3,1,2" 
Remove "77" from "6,20,9,10,19,101,5,3,1,2": "6,20,9,10,19,101,5,3,1,2" 
Remove "6" from "6"      : ""