在Java中每隔三个逗号分割一个String [英] Split a String at every 3rd comma in Java
问题描述
我有一个如下所示的字符串:
I have a string that looks like this:
0,0,1,2,4,5,3,4,6
我想要的回报是 String []
在每三个逗号后分割,所以结果如下所示:
What I want returned is a String[]
that was split after every 3rd comma, so the result would look like this:
[ "0,0,1", "2,4,5", "3,4,6" ]
我找到了类似的功能,但它们没有以第n个逗号分割。
I have found similar functions but they don't split at n-th amount of commas.
推荐答案
你可以尝试使用 split
方法与(?< = \\G\\\\ +,\\ \\\d +,\\\\ +),
正则表达式
You can try to use split
method with (?<=\\G\\d+,\\d+,\\d+),
regex
演示
String data = "0,0,1,2,4,5,3,4,6";
String[] array = data.split("(?<=\\G\\d+,\\d+,\\d+),"); //Magic :)
// to reveal magic see explanation below answer
for(String s : array){
System.out.println(s);
}
输出:
0,0,1
2,4,5
3,4,6
解释
-
\\d
表示一位数,与[0-9]相同,如0
或3
-
\\d +
表示一个或多个数字,如1
或23
-
\\d +,
表示后面带逗号的一个或多个数字,如1,
或234,
-
\ \ + +,\\\\ +,\\\\ +
将接受三个带逗号的数字,例如12,3,456
-
\\\\ G
表示最后一场比赛,或者如果没有(如果是第一次使用),则表示字符串的开头 -
(?< = ...),
是正面后瞻,它将匹配逗号,
即ha还有(?< = ...)
中描述的字符串 -
(?< ; = \\G \\\\ +,\\\\ +,\\\\ +),
所以会尝试找到前面有三个数字的逗号,这些数字有以前字符串的以太开始(例如^ 0,0,1
)或之前匹配的逗号,如2,4,5
和3,4,6
。
\\d
means one digit, same as [0-9], like0
or3
\\d+
means one or more digits like1
or23
\\d+,
means one or more digits with comma after it, like1,
or234,
\\d+,\\d+,\\d+
will accept three numbers with commas between them like12,3,456
\\G
means last match, or if there is none (in case of first usage) start of the string(?<=...),
is positive look-behind which will match comma,
that has also some string described in(?<=...)
before it(?<=\\G\\d+,\\d+,\\d+),
so will try to find comma that has three numbers before it, and these numbers have aether start of the string before it (like^0,0,1
in your example) or previously matched comma, like2,4,5
and3,4,6
.
如果您想使用其他字符,那么数字也可以使用其他字符集,例如
Also in case you want to use other characters then digits you can also use other set of characters like
-
\\\\
这将匹配字母字符,数字和_
-
\\\\
非空白的所有内容 -
[^,]
所有不是逗号的内容 - ...依此类推。更多信息,请访问模式文档
\\w
which will match alphabetic characters, digits and_
\\S
everything that is not white space[^,]
everything that is not comma- ... and so on. More info in Pattern documentation
顺便说一句,这个表格将在每3日,5日,7日分开工作,(和其他奇数)逗号,如 split((?< = \\G \\ w +,\\\\ + +,\\\\ + +,\\\ \\ w +,\\\\ +),)
将在每个第5个逗号分开。
By the way, this form will work with split on every 3rd, 5th, 7th, (and other odd numbers) comma, like split("(?<=\\G\\w+,\\w+,\\w+,\\w+,\\w+),")
will split on every 5th comma.
要在每个第2,第4,第6,第8(以及其余偶数)逗号分割,您需要替换 +
with {1,maxLengthOfNumber}
like split((?< = \\G\\\\ {1, 3},\\\ {1,3},\\\\ {1,3},\\\\ {1,3}),)
分开数字最多可以有3个数字(0,00,12,000,123,412,999)时的第4个逗号。
To split on every 2nd, 4th, 6th, 8th (and rest of even numbers) comma you will need to replace +
with {1,maxLengthOfNumber}
like split("(?<=\\G\\w{1,3},\\w{1,3},\\w{1,3},\\w{1,3}),")
to split on every 4th comma when numbers can have max 3 digits (0, 00, 12, 000, 123, 412, 999).
要分割每个第二个逗号,你也可以使用此正则表达式拆分((?<!\\\\\ + +),)
基于我的上一个答案
To split on every 2nd comma you can also use this regex split("(?<!\\G\\d+),")
based on my previous answer
这篇关于在Java中每隔三个逗号分割一个String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!