I need to split a string with regex in Java.
我需要用Java与regex分割一个字符串。
I need to get an array : A
, B
, 01/01/2016 12:31
, D
我需要得到一个数组:A, B, 01/2016 12:31, D
why my regex doesn't work ? it will output the original string.
为什么我的regex不起作用?它将输出原始字符串。
String source = "A|B|[01/01/2016 12:31]|D";
String regex = "\\|\\|\\[.*\\]\\|";
String[] array = source.split(regex);
for(String data: array){
System.out.println(data);
}
2 个解决方案
#1
2
The regex does not match the input string as it expects two consecutive pipes (at the start of the pattern). To be more precise, \|\|\[.*\]\|
matches 2 pipes followed by a [
followed with zero or more characters other than a newline (as many as possible) followed with ]
and a |
.
regex不匹配输入字符串,因为它期望两个连续的管道(在模式的开始)。更准确地说,|\|\[。\|匹配两个管道,后跟一个[后跟除换行符(尽可能多)以外的零或多个字符]和一个|。
You need to use the following regex:
您需要使用以下regex:
String regex = "[|\\[\\]]+";
See IDEONE demo
看到IDEONE演示
This regex will match one or more characters: |
, ]
or [
.
这个regex将匹配一个或多个字符:|,]或[。
#2
0
Modifying the 2nd line to something as below for the input string format and calling split with -1 should handle all situations when the tokens are empty including the last one
将第2行修改为如下所示的输入字符串格式,并调用split with -1,应该可以处理所有令牌为空的情况,包括最后一个令牌
String regex = "((\\|\\[)|(\\]\\|)|(\\|))";
String[] array = source.split(regex, -1);
the above gives following output for 'A|B|[01/01/2016 12:31]|D' in case of empty D as well it prints a zero string.
上面给出了“A|B|[01/ 2016 12:31]|D”的输出,如果D为空,它也会打印一个零字符串。
A
B
01/01/2016 12:31
D
This though will work for input strings of the format A|B|[C]|D, the other regex mentioned on the thread [[|\[\]]]+ will handle square brackets showing anywhere in the input along with | separator as long as there are no zero strings, for example A||[C]|D will result in shifting of tokens.
这将适用于格式A|B|[C]|D的输入字符串,线程上提到的另一个regex[[[[|\[\]]]]+将处理显示输入任何地方的方括号以及|分隔符,只要没有任何字符串,例如||[C]|D将导致令牌移位。
#1
2
The regex does not match the input string as it expects two consecutive pipes (at the start of the pattern). To be more precise, \|\|\[.*\]\|
matches 2 pipes followed by a [
followed with zero or more characters other than a newline (as many as possible) followed with ]
and a |
.
regex不匹配输入字符串,因为它期望两个连续的管道(在模式的开始)。更准确地说,|\|\[。\|匹配两个管道,后跟一个[后跟除换行符(尽可能多)以外的零或多个字符]和一个|。
You need to use the following regex:
您需要使用以下regex:
String regex = "[|\\[\\]]+";
See IDEONE demo
看到IDEONE演示
This regex will match one or more characters: |
, ]
or [
.
这个regex将匹配一个或多个字符:|,]或[。
#2
0
Modifying the 2nd line to something as below for the input string format and calling split with -1 should handle all situations when the tokens are empty including the last one
将第2行修改为如下所示的输入字符串格式,并调用split with -1,应该可以处理所有令牌为空的情况,包括最后一个令牌
String regex = "((\\|\\[)|(\\]\\|)|(\\|))";
String[] array = source.split(regex, -1);
the above gives following output for 'A|B|[01/01/2016 12:31]|D' in case of empty D as well it prints a zero string.
上面给出了“A|B|[01/ 2016 12:31]|D”的输出,如果D为空,它也会打印一个零字符串。
A
B
01/01/2016 12:31
D
This though will work for input strings of the format A|B|[C]|D, the other regex mentioned on the thread [[|\[\]]]+ will handle square brackets showing anywhere in the input along with | separator as long as there are no zero strings, for example A||[C]|D will result in shifting of tokens.
这将适用于格式A|B|[C]|D的输入字符串,线程上提到的另一个regex[[[[|\[\]]]]+将处理显示输入任何地方的方括号以及|分隔符,只要没有任何字符串,例如||[C]|D将导致令牌移位。