Reputation: 2869
I'm trying to split a String in Java. The splits should occur in between two characters one of which is an alphabetic one (a-z, A-Z) and the other numeric (0-9). For example:
String s = "abc123def456ghi789jkl";
String[] parts = s.split(regex);
System.out.println(Arrays.deepToString(parts));
Output should be [abc, 123, def, 456, ghi, 789, jkl]
. Can someone help me out with a matching regular expression?
Thanks in advance!
Upvotes: 5
Views: 372
Reputation: 46209
You can use a combination of look-ahead and look-behind:
String s = "abc123def456ghi789jkl";
String regex = "(?<=\\d)(?=[a-z])|(?<=[a-z])(?=\\d)";
String[] parts = s.split(regex);
System.out.println(Arrays.deepToString(parts));
Upvotes: 9