Use lookarounds: str.split("(?<=\\d)(?=\\D)")
String[] parts = "123XYZ".split("(?<=\\d)(?=\\D)");
System.out.println(parts[0] + "-" + parts[1]);
// prints "123-XYZ"
\d
is the character class for digits; \D
is its negation. So this zero-matching assertion matches the position where the preceding character is a digit (?<=\d)
, and the following character is a non-digit (?=\D)
.
References
Related questions
Alternate solution using limited split
The following also works:
String[] parts = "123XYZ".split("(?=\\D)", 2);
System.out.println(parts[0] + "-" + parts[1]);
This splits just before we see a non-digit. This is much closer to your original solution, except that since it doesn't actually match the non-digit character, it doesn't "eat it up". Also, it uses limit
of 2
, which is really what you want here.
API links
String.split(String regex, int limit)
- If the limit
n
is greater than zero then the pattern will be applied at most n - 1
times, the array's length will be no greater than n
, and the array's last entry will contain all input beyond the last matched delimiter.