在Java String.split()方法中使用转义字符处理分隔符

时间:2021-04-17 22:07:47

I have searched the web for my query, but didn't get the answer which fits my requirement exactly. I have my string like below:

我在网上搜索了我的查询,但没有得到完全符合我要求的答案。我有我的字符串如下:

A|B|C|The Steading\|Keir Allan\|Braco|E

My Output should look like below:

我的输出应如下所示:

A
B
C
The Steading|Keir Allan|Braco
E

My requirement is to skip the delimiter if it is preceded by the escape sequence. I have tried the following using negative lookbehinds in String.split():

我的要求是跳过分隔符,如果前面有转义序列。我在String.split()中使用负面lookbehinds尝试了以下方法:

(?<!\\)\|

But, my problem is the delimiter will be defined by the end user dynamically and it need not be always |. It can be any character on the keyboard (no restrictions). Hence, my doubt is that the above regex might fail for some of the special characters which are not allowed in regex.

但是,我的问题是分隔符将由最终用户动态定义,并且不必总是|。它可以是键盘上的任何字符(没有限制)。因此,我怀疑上述正则表达式可能会因正则表达式中不允许的某些特殊字符而失败。

I just wanted to know if this is the perfect way to do it.

我只是想知道这是否是完美的方式。

1 个解决方案

#1


25  

You can use Pattern.quote():

你可以使用Pattern.quote():

String regex = "(?<!\\\\)" + Pattern.quote(delim);

Using your example:

使用你的例子:

String delim = "|";
String regex = "(?<!\\\\)" + Pattern.quote(delim);

for (String s : "A|B|C|The Steading\\|Keir Allan\\|Braco|E".split(regex))
    System.out.println(s);
A
B
C
The Steading\|Keir Allan\|Braco
E

You can extend this to use a custom escape sequence as well:

您可以扩展它以使用自定义转义序列:

String delim = "|";
String esc = "+";
String regex = "(?<!" + Pattern.quote(esc) + ")" + Pattern.quote(delim);

for (String s : "A|B|C|The Steading+|Keir Allan+|Braco|E".split(regex))
    System.out.println(s);
A
B
C
The Steading+|Keir Allan+|Braco
E

#1


25  

You can use Pattern.quote():

你可以使用Pattern.quote():

String regex = "(?<!\\\\)" + Pattern.quote(delim);

Using your example:

使用你的例子:

String delim = "|";
String regex = "(?<!\\\\)" + Pattern.quote(delim);

for (String s : "A|B|C|The Steading\\|Keir Allan\\|Braco|E".split(regex))
    System.out.println(s);
A
B
C
The Steading\|Keir Allan\|Braco
E

You can extend this to use a custom escape sequence as well:

您可以扩展它以使用自定义转义序列:

String delim = "|";
String esc = "+";
String regex = "(?<!" + Pattern.quote(esc) + ")" + Pattern.quote(delim);

for (String s : "A|B|C|The Steading+|Keir Allan+|Braco|E".split(regex))
    System.out.println(s);
A
B
C
The Steading+|Keir Allan+|Braco
E