I have a String str1 below written in Java that I would like to split.
String str1 = "S1..R1..M1..D2..N3..S1.R1.M1.D2.N3.S1R1M1D2N3";
I would like to split the string into following elements in an Array:
S1.., R1.., M1.., D2.., N3.., S1., R1., M1., D2, N3., S1, R1, M1, D2, N3
I guess I have to go for 3 pass splitting, first with .., next with . and finally with letter.
First I tried to split with .., but I do not get expected result:
System.out.println("\n Original String = "+str1+"\nSplit Based on .. = "+Arrays.toString(str1.split("(?<=[..])")));
The result of the above split is:
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]
I tried even with:
("(?<=[.+])").
Not sure if I need to go for Pattern/Matches.
Need your help please.
Instead of using Positive Lookbehind, use Positive Lookahead.
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));
Note: I used Negative Lookbehind before the lookahead assertion to assert it's impossible to match the position at start of the string. By doing this it prevents an empty element as the first item in your list.
Output
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]
Another way is to match instead of split.
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);
Output
[S1.., R1.., M1.., D2.., N3.., S1., R1., M1., D2., N3., S1, R1, M1, D2, N3]
Pass in an intelligent regex for the argument of .split(). I'm going to enlighten you and provide you this intelligent regex. ;)
str1.split("(?<=[.\\d])(?=[A-Z]\\d)")
Takes:
"S1..R1..M1..D2..N3..S1.R1.M1.D2.N3.S1R1M1D2N3"
Gives:
["S1..", "R1..", "M1..", "D2..", "N3..", "S1.", "R1.", "M1.", "D2.", "N3.", "S1", "R1", "M1", "D2", "N3"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With