以下是我的意见:
我的in-1 @ 123pu *(46t789
这是我的代码:
public static void main(String[] args) {
String input = "my in-1@123pu*(456t789";
String[] words = input.split("(\\s+|(?:(?<=[A-Za-z])(?=[0-9])|(?<=[0-9])(?=[A-Za-z])))");
String output = "";
for (String s : words) {
output += s+" ";
}
System.out.println(output);
}
代码执行输出为:
我的in-1 @ 123 pu *(456 t 789
如何获得这样的输出:
我的 - 1 @ 123 pu *(456 t 789
我需要在特殊字符和数字之间添加一个分隔符(例如空格)。
答案 0 :(得分:0)
使用此
String[] ww = input.split("\\s+" + // consume white space
"|(?:" + // start non-capture
"(?<=[^a-zA-Z0-9 ])" + // look behind non alphanumeric & white space
"|(?=[^a-zA-Z0-9 ])" + // look ahead non alphanumeric & white space
"|((?=[0-9])(?<![0-9 ]))" + // ahead is number, behind is letter
"|((?=[a-zA-Z])(?<![a-zA-Z ]))" + // ahead is letter, behind is number
")");
结果:
my in - 1 @ 123 pu * ( 456 t 789