Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting based on double dots excluding single dot in Java

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.

like image 699
Mahesha Padyana Avatar asked Apr 08 '26 03:04

Mahesha Padyana


2 Answers

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]
like image 115
hwnd Avatar answered Apr 09 '26 17:04

hwnd


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"]
like image 33
Unihedron Avatar answered Apr 09 '26 15:04

Unihedron



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!