RegEx match between characters

I am trying to split String in Java. Separations must occur between two characters, one of which is alphabetic (az, AZ) and the other number (0-9). For instance:

String s = "abc123def456ghi789jkl";
String[] parts = s.split(regex);
System.out.println(Arrays.deepToString(parts));

The conclusion should be [abc, 123, def, 456, ghi, 789, jkl]. Can someone help me with the appropriate regex?

Thanks in advance!

+5
source share
1 answer

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));
+9
source

All Articles