我有一个用Java编写的String str1,我想拆分。
String str1 = "S1..R1..M1..D2..N3..S1.R1.M1.D2.N3.S1R1M1D2N3";
我想将字符串拆分为数组中的以下元素:
S1.., R1.., M1.., D2.., N3.., S1., R1., M1., D2, N3., S1, R1, M1, D2, N3
我想我必须进行3次传球分裂,首先是......,然后是。最后用信。
首先我尝试用..分开,但我没有得到预期的结果:
System.out.println("\n Original String = "+str1+"\nSplit Based on .. = "+Arrays.toString(str1.split("(?<=[..])")));
上述分割的结果是:
Original String = S1..R1..M1..D2..N3..S1.R1.M1.D2.N3.S1R1M1D2N3
Split Based on .. = [S1., ., R1., ., M1., ., D2., ., N3., ., S1., R1., M1., D2., N3., S1R1M1D2N3]
我甚至尝试过:
("(?<=[.+])").
不确定我是否需要进行模式/匹配。
请你帮忙。
答案 0 :(得分:5)
String s = "S1..R1..M1..D2..N3..S1.R1.M1.D2.N3.S1R1M1D2N3";
String[] parts = s.split("(?<!\\A)(?=[A-Z]\\d)");
System.out.println("Original = " + s + "\nSplitted = " + Arrays.toString(parts));
注意:在前瞻断言之前我使用了Negative Lookbehind来断言它不可能匹配字符串开头的位置。通过这样做,它可以防止空元素作为列表中的第一项。
<强>输出强>
Original = S1..R1..M1..D2..N3..S1.R1.M1.D2.N3.S1R1M1D2N3
Splitted = [S1.., R1.., M1.., D2.., N3.., S1., R1., M1., D2., N3., S1, R1, M1, D2, N3]
另一种方法是匹配而不是拆分。
String s = "S1..R1..M1..D2..N3..S1.R1.M1.D2.N3.S1R1M1D2N3";
Pattern p = Pattern.compile("[A-Z]\\d+\\.*");
Matcher m = p.matcher(s);
List<String> matches = new ArrayList<String>();
while (m.find()) {
matches.add(m.group());
}
System.out.println(matches);
<强>输出强>
[S1.., R1.., M1.., D2.., N3.., S1., R1., M1., D2., N3., S1, R1, M1, D2, N3]
答案 1 :(得分:2)
为.split()
的论点传递一个智能正则表达式。我将启发你并为你提供这个聪明的正则表达式。 ;)
str1.split("(?<=[.\\d])(?=[A-Z]\\d)")
注意到:
"S1..R1..M1..D2..N3..S1.R1.M1.D2.N3.S1R1M1D2N3"
给出:
["S1..", "R1..", "M1..", "D2..", "N3..", "S1.", "R1.", "M1.", "D2.", "N3.", "S1", "R1", "M1", "D2", "N3"]