hielsnoppe
hielsnoppe

Reputation: 2869

RegEx matching between characters

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

Answers (1)

Keppil
Keppil

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

Related Questions