在Java中每隔三个逗号分割一个String [英] Split a String at every 3rd comma in Java

查看:2780
本文介绍了在Java中每隔三个逗号分割一个String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的字符串:

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], like 0 or 3
  • \\d+ means one or more digits like 1 or 23
  • \\d+, means one or more digits with comma after it, like 1, or 234,
  • \\d+,\\d+,\\d+ will accept three numbers with commas between them like 12,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, like 2,4,5 and 3,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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆