2011-07-29 62 views
6

我有點卡住試圖想出正則表達式分手字符串具有以下屬性:java的正則表達式分割字符串

  1. 由分隔| (管)字符
  2. 如果某個值包含一個管道,以\(反斜槓)
  3. 逃脫如果是個人價值與反斜槓結尾,用反斜線

因此,舉例來說,這裏有一些字符串我想分手:

  1. One|Two|Three應該產生:["One", "Two", "Three"]
  2. One\|Two\|Three應該產生:["One|Two|Three"]
  3. One\\|Two\|Three應該產生:["One\", "Two|Three"]

現在我怎麼可能分裂這個了一個單一的正則表達式?

更新:許多人已經建議,這不是一個好的正則表達式的應用程序。此外,正則表達式解決方案比僅遍歷字符慢幾個數量級。我結束了迭代字符:

public static List<String> splitValues(String val) { 
    final List<String> list = new ArrayList<String>(); 
    boolean esc = false; 
    final StringBuilder sb = new StringBuilder(1024); 
    final CharacterIterator it = new StringCharacterIterator(val); 
    for(char c = it.first(); c != CharacterIterator.DONE; c = it.next()) { 
     if(esc) { 
      sb.append(c); 
      esc = false; 
     } else if(c == '\\') { 
      esc = true; 
     } else if(c == '|') { 
      list.add(sb.toString()); 
      sb.delete(0, sb.length()); 
     } else { 
      sb.append(c); 
     } 
    } 
    if(sb.length() > 0) { 
     list.add(sb.toString()); 
    } 
    return list; 
} 
+1

讓我們說清楚。你想要的是:用|分割並將其從字符串中刪除,請勿按\ |分隔並從字符串中刪除\,最後由\\ |分隔並刪除\ |從第一部分和從第二部分。你如何認爲這可以用一個正則表達式來完成?這對我來說似乎是完全不同的情況...... – user219882

+0

是否可以更改您的分隔符? – Paul

+0

我想你們是對的!正則表達式可能太多了。 –

回答

13

訣竅是不使用split()方法。這會迫使你使用lookbehind來檢測轉義字符,但是當轉義本身被轉義時(如你發現的),失敗了。您需要使用find()代替,以匹配令牌而不是分隔符:

public static List<String> splitIt(String source) 
{ 
    Pattern p = Pattern.compile("(?:[^|\\\\]|\\\\.)+"); 
    Matcher m = p.matcher(source); 
    List<String> result = new ArrayList<String>(); 
    while (m.find()) 
    { 
    result.add(m.group().replaceAll("\\\\(.)", "$1")); 
    } 
    return result; 
} 

public static void main(String[] args) throws Exception 
{ 
    String[] test = { "One|Two|Three", 
        "One\\|Two\\|Three", 
        "One\\\\|Two\\|Three", 
        "One\\\\\\|Two" }; 
    for (String s :test) 
    { 
    System.out.printf("%n%s%n%s%n", s, splitIt(s)); 
    } 
} 

輸出:

One|Two|Three 
[One, Two, Three] 

One\|Two\|Three 
[One|Two|Three] 

One\\|Two\|Three 
[One\, Two|Three] 

One\\\|Two 
[One\|Two] 
+0

令人印象深刻。你能解釋這種模式是如何工作的嗎?我仍然在與正則表達式鬥爭。 WOW !! – Paul

+0

這很甜蜜!我知道正則表達式可以做到這一點:-) –

+0

這就像一個魅力!再次感謝@Alan Moore!現在你會怎麼做呢? –

相關問題