I’m trying to work out a way of splitting up a string in java that follows a pattern like so:
String a = "123abc345def";
The results from this should be the following:
x[0] = "123";
x[1] = "abc";
x[2] = "345";
x[3] = "def";
However I’m completely stumped as to how I can achieve this. Please can someone help me out? I have tried searching online for a similar problem, however it’s very difficult to phrase it correctly in a search.
Please note: The number of letters & numbers may vary (e.g. There could be a string like so ‘1234a5bcdef’)
You could try to split on
(?<=\D)(?=\d)|(?<=\d)(?=\D), like:It matches positions between a number and not-a-number (in any order).
(?<=\D)(?=\d)– matches a position between a non-digit (\D) and a digit (\d)(?<=\d)(?=\D)– matches a position between a digit and a non-digit.