我正在尝试在Java中拆分String。分割应该出现在两个字符之间,其中一个字符是字母字符(a-z,A-Z),另一个是数字字符(0-9)。例如:
String s = "abc123def456ghi789jkl";
String[] parts = s.split(regex);
System.out.println(Arrays.deepToString(parts));
输出应为[abc, 123, def, 456, ghi, 789, jkl]
。有人可以帮我找到匹配的正则表达式吗?
提前致谢!
答案 0 :(得分:9)
您可以使用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));