拆分后,才逗号3次出现在Java中 [英] Split only after comma 3 times appear in Java

查看:194
本文介绍了拆分后,才逗号3次出现在Java中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在谷歌上搜索永远和根本无法找到一个解决方案。

I have been googling forever and simply couldn't find a solution.

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

I have a string that looks like this:

0,0,1,2,4,5,3,4,6

0,0,1,2,4,5,3,4,6

我要的是返回被每3个逗号分割后的字符串[],所以它看起来是这样的:

What I want returned is a string[] that was split after every 3rd comma, so it would look like this:

[0,0,1,2,4,5,3,4,6]

[ "0,0,1", "2,4,5", "3,4,6" ]

我已经找到similiar功能,但他们都没有用逗号的第n个分割量

I have found similiar functions but they all don't split with the nth amount of commas.

真的AP preciate任何帮助我能!

Would really appreciate any help I can get!

推荐答案

您可以尝试使用拆分(小于?= \\ \\ 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

说明


  • \\\\ð表示一个数字,等同于[0-9],如 0 3

  • \\\\ D + 是指一个或多个数字像 1 23

  • \\\\ D +,是指一个或多个数字用逗号后,像 1 234

  • \\\\ D + \\\\ D + \\\\ D + 将接受它们之间的逗号像 12,3,456

  • \\\\摹表示最后一场比赛,或者如果没有(在第一次使用的情况下)的字符串开始

  • ; 是的正向后看将匹配逗号有也>在之前

  • (小于?= \\\\摹\\\\ D + \\\\ D +,\\\\ D +),所以会设法找到它之前有三个数字的逗号,而这些数字的字符串的以太网开始之前(如 ^ 0,0,1 在你的例子)或previously匹配逗号,如 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日,(和其他奇数)逗号,如拆分((小于工作\\\\ 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} 拆分((小于= \\\\摹\\\\ W {1,3},\\\\ W {1,3},\\\\ W { 1,3},\\\\ W {1,3}))来分割的每4个逗号的数字时,可以有最多3个数字(0,00,12,000,123,412, 999)。

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逗号,你也可以使用这个正则表达式拆分((小于?!\\\\摹\\\\ D +))根据我的< A HREF =htt​​p://stackoverflow.com/a/16486373/1393766> previous答案

To split on every 2nd comma you can also use this regex split("(?<!\\G\\d+),") based on my previous answer

这篇关于拆分后,才逗号3次出现在Java中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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