I would use string.split
function.
String s = "My Street 10 90210 Beverly Hills";
String parts[] = s.split("\\s+(?=\\d+\\s+\\d+)|(?<=\\d+)\\s+(?=[A-Z])|(?<=\\d+)\\s+(?=\\d+)");
System.out.println(Arrays.toString(parts));
Output:
[My Street, 10, 90210, Beverly Hills]
Explanation:
\\s+(?=\\d+\\s+\\d+)
Matches one or more spaces only if it's followed by one or more digits plus one or more spaces plus one or more digits. So that space before house number would satisfy this condition . So it got matched.
|
Called alternation operator.
(?<=\\d+)\\s+(?=[A-Z])
Matches one or more spaces which are preceded by one or more digits and then followed by a capital letter. So the spaces before the string city
would satisfy this condition and got matched.
(?<=\\d+)\\s+(?=\\d+)
This matches all the spaces which are in-between the digits. So the spaces between house-number and zip-code got matched.
Splitting your input according to the matched spaces will give you the desired output.