如何在忽略/?的情况下用指定的分隔符拆分java中的字符串? [重复]

时间:2021-07-26 21:43:51

This question already has an answer here:

这个问题在这里已有答案:

I tried to read about regex and escaping, but no luck.

我试着阅读关于正则表达式和逃避,但没有运气。

I have a string that looks like this:

我有一个看起来像这样的字符串:

String s = "4/18/2015|Planned|Linux|Maintenance";

And I want to split it with the delimiter '|' :

我想用分隔符'|'拆分它:

String[] tokens = s.split("|");

The correct results I am expecting which are

我期待的正确结果是什么

tokens[0] is "4/18/2015", 
tokens[1] is "Planned", 
tokens[2] is "Linux", 
token[3] is "Maintenance", 

yet it's giving me some weird result like this:

然而它给了我一些奇怪的结果:

tokens[0] is null
tokens[1] is 4
tokens[2] is /
and tokens[3] is 1

I am guessing it's because of the slashes '/' in the date that's why. I tried to search for many existing questions and tried the suggested methods as well but to no avail.

我猜这是因为日期中的斜线'/'就是为什么。我试图搜索许多现有的问题,并尝试了建议的方法,但无济于事。

3 个解决方案

#1


3  

Just use

只是用

split("\\x7C")

or

要么

split("\\|")

You need to escape or use corresponding unicode value when splitting against the pipeline char '|'.

在拆分管道char'|'时,您需要转义或使用相应的unicode值。

#2


4  

@mushfek0001 got it right.

@ mushfek0001做对了。

The pipe in most regex dialects is a metacharacter for the alternation; basically what you ask the regex engine to do here is: "split against the empty string or... the empty string".

大多数正则表达方言中的管道是交替的元字符;基本上你要求正则表达式引擎在这里做的是:“分裂空字符串或...空字符串”。

And, uh, it means you would potentially get empty each time, except that the regex engine is not a fool, and if an empty match is detected in a split the engine will advance one character before splitting again... Hence your result (not sure why the first element is null and not the empty string, though).

并且,呃,这意味着你每次都可能变空,除了正则表达式引擎不是傻瓜,如果在分割中检测到空匹配,引擎将在再次分裂之前前进一个字符...因此你的结果(不知道为什么第一个元素为null而不是空字符串。

Therefore, you should split against "\\|", not "|".

因此,您应该拆分“\\ |”,而不是“|”。

What is more, if you do this repeatedly, use a Pattern instead:

更重要的是,如果你反复这样做,请改用Pattern:

private static final Pattern PIPE = Pattern.compile("\\|");

// ...

final String[] tokens = PIPE.split(yourInput);

#3


2  

escape the pipe character:

逃避管道角色:

s.split("\\|");

because pipe sign in regex means OR, so to escape it you need \| but in regex you need to escape \ too so \\| will work.

因为管道登录正则表达式是OR,所以为了逃避它你需要\ |但是在正则表达式中,你需要逃避\ \所以\\ |将工作。

or as mushfek0001 suggested:

或作为mushfek0001建议:

split("\\x7C")

#1


3  

Just use

只是用

split("\\x7C")

or

要么

split("\\|")

You need to escape or use corresponding unicode value when splitting against the pipeline char '|'.

在拆分管道char'|'时,您需要转义或使用相应的unicode值。

#2


4  

@mushfek0001 got it right.

@ mushfek0001做对了。

The pipe in most regex dialects is a metacharacter for the alternation; basically what you ask the regex engine to do here is: "split against the empty string or... the empty string".

大多数正则表达方言中的管道是交替的元字符;基本上你要求正则表达式引擎在这里做的是:“分裂空字符串或...空字符串”。

And, uh, it means you would potentially get empty each time, except that the regex engine is not a fool, and if an empty match is detected in a split the engine will advance one character before splitting again... Hence your result (not sure why the first element is null and not the empty string, though).

并且,呃,这意味着你每次都可能变空,除了正则表达式引擎不是傻瓜,如果在分割中检测到空匹配,引擎将在再次分裂之前前进一个字符...因此你的结果(不知道为什么第一个元素为null而不是空字符串。

Therefore, you should split against "\\|", not "|".

因此,您应该拆分“\\ |”,而不是“|”。

What is more, if you do this repeatedly, use a Pattern instead:

更重要的是,如果你反复这样做,请改用Pattern:

private static final Pattern PIPE = Pattern.compile("\\|");

// ...

final String[] tokens = PIPE.split(yourInput);

#3


2  

escape the pipe character:

逃避管道角色:

s.split("\\|");

because pipe sign in regex means OR, so to escape it you need \| but in regex you need to escape \ too so \\| will work.

因为管道登录正则表达式是OR,所以为了逃避它你需要\ |但是在正则表达式中,你需要逃避\ \所以\\ |将工作。

or as mushfek0001 suggested:

或作为mushfek0001建议:

split("\\x7C")