简体   繁体   English

在Java上拆分字符串“;”,而不是“\\\\;”

[英]Splitting a string in Java on “;”, but not on “\\;”

In Java I try try to use the String.split() method splitting a string on ";" 在Java中我尝试使用String.split()方法在";"上拆分字符串 , but not on "\\\\\\\\;" ,但不是"\\\\\\\\;" . (2 back-slashes followed by a semi-colon) (2个反斜杠后跟分号)

Ex: "aa;bb;cc\\\\;dd;ee\\\\;;ff" should be split into; 例如: "aa;bb;cc\\\\;dd;ee\\\\;;ff"应分成;

aa

bb

cc\\;dd

ee\\;

ff

How do I accomplish this using a regular expression? 如何使用正则表达式完成此操作?

Markus 马库斯

Use 采用

"aa;bb;cc\\;dd;ee\\;;ff".split("(?<!\\\\);");

(?<!...) is called a "zero-width lookbehind". (?<!...)被称为“零宽度后视”。 In English, you're splitting on all ; 在英语中,你是分裂的; characters that are NOT preceded by a double slash, without actually matching the double slash. 带双斜杠的字符,实际上不匹配双斜杠。 The quadruple slash is to escape backslashes to the regex parser. 四重斜杠是将反斜杠转义为正则表达式解析器。 The actual regular expression used in the split would then read: 然后,拆分中使用的实际正则表达式将读取:

(?<!\\);

This is called negative lookbehind and the syntax is like (?<!a)b . 这称为负后观,语法类似于(?<!a)b This matches on any b that isnt precended by an a. 这匹配在a之前的任何b上。 You would want something like: 你会想要这样的东西:

(?<!\\\\);

Here a code example with . 这里有一个代码示例。 as separator: 作为分隔符:

String p = "hello.regex\\.brain\\.twister";
System.out.println( p );
for (String s : p.split( "(?<!\\\\)\\.", -1 )) {
  System.out.println( "-> "+ s );
}

Will Ouptut: Will Ouptut:

hello.regex\.brain\.twister
-> hello
-> regex\.brain.\twister

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM