我有点卡住试图想出正则表达式分手字符串具有以下属性:java的正则表达式分割字符串
- 由分隔| (管)字符
- 如果某个值包含一个管道,以\(反斜杠)
- 逃脱如果是个人价值与反斜杠结尾,用反斜线
因此,举例来说,这里有一些字符串我想分手:
One|Two|Three
应该产生:["One", "Two", "Three"]
One\|Two\|Three
应该产生:["One|Two|Three"]
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;
}
让我们说清楚。你想要的是:用|分割并将其从字符串中删除,请勿按\ |分隔并从字符串中删除\,最后由\\ |分隔并删除\ |从第一部分和从第二部分。你如何认为这可以用一个正则表达式来完成?这对我来说似乎是完全不同的情况...... – user219882
是否可以更改您的分隔符? – Paul
我想你们是对的!正则表达式可能太多了。 –