-4

I am not able to split the string for every 1000th comma in a string using regular expression. We are able to split for every 3rd comma in string but not for 1000th comma.

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);
}

Thanks in advance for your suggestions.

Shiva kumar
  • 13
  • 1
  • 4
  • 3
    why with regex? And ask yourself: "Do I understand what the regex in the question does?" – DeiAndrei Nov 24 '17 at 12:58
  • Try to find the index of 1000th comma using a loop, then do a substring. – Tarun Gupta Nov 24 '17 at 13:01
  • How is this question is a duplicate! Unless of course you think the OP should place `\\d+,` 1000 times within his/her regex string (which I believe wont work either). Read the question. Has anyone tried it to accomplish this with the accepted answer within the duplicate? I think the OP initially got the code there. – DevilsHnd - 退職した Nov 24 '17 at 22:53
  • @DevilsHnd look at the second, third, etc. answers. The data is also a dupe, just the end result is different – ctwheels Nov 24 '17 at 22:59
  • It is the end result the OP wants to accomplish...split on every 1000th delimiter. Granted, some of the other answers may accomplish the task. – DevilsHnd - 退職した Nov 24 '17 at 23:02

1 Answers1

1

This is probably the regex you're looking for:

 (\w*,){3}\K\S+

  \w             Matches a word ([a-zA-Z0-9_])
  \w*            Matches 0 or infinite number of words...
  \w*,           ... Followed by a coma
 (\w*,){n}       Match this group, n times
          \K     Reset the starting point of the match after the n elements
            \S+  Matches any word after the (n-1)th coma

Where n is the number of elements, plus 1. So in your case, it would be 1001.

With that being said, regex is probably not the right tool for this :)

Nepho
  • 1,074
  • 1
  • 11
  • 32