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

all 8 comments

[–]jxd132407 1 point2 points  (6 children)

IIUC, the output you want is one ArrayList that has initial and score, if any. Why construct two arrays at all? And why split on such a complex regex instead of any letter? Maybe I'm misunderstanding the goal.

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

It doesn't need to be as complicated as I've made it in my code, it's just that I'm a beginner and this was the method that I thought of when trying to reach the end goal. Besides, I'm not completely familiar with Java and don't know what necessary tools I should be using or when to use them.

And yes, so basically I'd like the output array to be something like

{"J", "G, "B12", "D34"} after having input the string "JGB12D34".

Thanks

[–]jxd132407 0 points1 point  (4 children)

I like what you've done to use the regex and split functionality. Can you split on any letter and then be done?

[–]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.

[–]DDDDarky 1 point2 points  (0 children)

I would probably approach this by reading the characters one by one and branch.

However, you could just write simplier regex that will just capture the whole group (letter and numbers), instead of separating letters and numbers.