2011-07-29 59 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!现在你会怎么做呢? –

相关问题