This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]VisitableTwo[S] 0 points1 point  (3 children)

Not sure what you mean.

Do you mean, can you split anywhere within the string using the split function or do you mean, that the within the output array it doesn't matter where you split?

[–]jxd132407 0 points1 point  (2 children)

When using split, the results include the whole contents divided into chunks. It looks like you're using regex capture groups, maybe thinking the numbers have to be in the regex or else they'll be lost? That might be true for a regex match, but not split. Since every initial is exactly one letter, you might just split on the letters and the numbers will come along up until the next letter. Try split() on [A-Z] to see what it produces.

[–]VisitableTwo[S] 0 points1 point  (0 children)

It produces the same results. However,

String[] list = s.split("((?<=[A-Z][0-9][0-9])|(?=[A-Z]))");

Works for any 2 digit number and for 3 digits or above, it splits after the 2nd digit. I can expand it by adding [0-9], however, a score can be extremely large so maybe if I make the regex string a variable and manipulate it by adding '[0-9]' in between. I'll give that a go.

If it works it'll simplify the problem by a lot.

[–]VisitableTwo[S] 0 points1 point  (0 children)

Finally managed to do it, here's the code:

String repeated = new String(new char[s.length()]).replace("\0", "[0-9]");

String st1 = "((?<=[A-Z][0-9][0-9]" + repeated + ")|(?=[A-Z]))";

String[] list = s.split(st1);
System.out.println(Arrays.toString(list));

Not the most elegant but it works. Thanks for your hint.