Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java split with certain patern

Tags:

java

regex

String abc ="abc_123,low,101.111.111.111,100.254.132.156,abc,1";
String[] ab = abc.split("(\\d+),[a-z]");
System.out.println(ab[0]);

Expected Output:

abc_123
low
101.111.111.111,100.254.132.156
abc
1

The problem is i am not able to find appropriate regex for this pattern.

like image 395
Krish Krishna Avatar asked Dec 25 '22 18:12

Krish Krishna


1 Answers

I would suggest to not solve all problems with one regular expression.

It seems that your initial string contains values that are separated by ",". So split those values with ",".

Then iterate the output of that process; and "join" those elements that are IP addresses (as it seems that this is what you are looking for).

And just for the sake of it: keep in mind that IP addresses are actually pretty complicated; a pattern "to match em all" can be found here

like image 177
GhostCat Avatar answered Jan 16 '23 15:01

GhostCat