如何将此字符串拆分为两个

时间:2016-03-09 15:49:58

标签: java regex

我有一个字符串= 123456789\ABC 123\ABC123

使用正则表达式我将其拆分为两个。输出应该如下所示

Group1 = 123456789 Group2 = 123\ABC123

1 个答案:

答案 0 :(得分:0)

尽量不要完全改变你的问题,使所有答案都无用。话虽如此,这是解决(新)问题的一种方法:

String str= "123456789\ABC 123\ABC123";

//"\\\\" is required to properly escape the backslash character
String[] split =  str.split("\\\\",2); //splits into 2 

String group1 = split[0];

//split[1] would now contain the String "ABC 123\ABC123". split it again by whitespace character to obtain `group2`
String[] temp = split[1].split("\\s+",2);
String group2 = temp[1];