Java中每第三个逗号分割一个字符串 [英] 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+,\\d+,\\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,
\\d+,\\d+,\\d+
将接受三个数字,它们之间有逗号,如12,3,456
\\G
表示最后一次匹配,或者如果没有(在第一次使用的情况下)字符串开头(?<=...),
是 positive look-behind 将匹配逗号,
也有一些在它之前的(?<=...)
中描述的字符串(?<=\\G\\d+,\\d+,\\d+),
所以会尝试找到前面有三个数字的逗号,并且这些数字以以太开头之前的字符串(如您的示例中的^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
将匹配字母字符、数字和_
\\S
不是空白的所有内容[^,]
不是逗号的所有内容- ...等等.模式文档
\\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+,\\w+,\\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(以及其余偶数)逗号上拆分,您需要将 +
替换为 {1,maxLengthOfNumber}
,例如 <代码>split("(?<=\\G\\w{1,3},\\w{1,3},\\w{1,3},\\w{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).
要拆分每 2 个逗号,您还可以根据我的 以前的答案
To split on every 2nd comma you can also use this regex split("(?<!\\G\\d+),")
based on my previous answer
这篇关于Java中每第三个逗号分割一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!