Split String deppending on characters in Java

Jürgen K. :

Given is the following string (one line):

a_Type_of_Data 0.847481:611x569+446+1200,0.515323:597x762+448+1101,0.587354:597x558+488+1207

I would like to split it like this [611, 559, 446 ,1200], [597, 762, 448, 1101], [597, 558, 488, 1207] So basically to get the 4 numbers between each ":" and "," regardless of the characters between the numbers.

I already tried to split the string on "," but then I still have the problem of the number in front of ":". Or the all go together like usingstring result = Regex.Replace(input, @"[^\d]", "");

Is there a way to do it with regex? Thanks in advance.

Pushpesh Kumar Rajwanshi :

You can first use this regex \d+x\d+(?:\+\d+){2} to match those numbers separated by x and + and then split those individual strings using [x+] which will give you another array of numbers.

Check this Java code,

String s = "a_Type_of_Data 0.847481:611x569+446+1200,0.515323:597x762+448+1101,0.587354:597x558+488+1207";
Pattern p = Pattern.compile("\\d+x\\d+(?:\\+\\d+){2}");
List<List<String>> list = new ArrayList<>();

Matcher m = p.matcher(s);
while(m.find()) {
    String[] data = m.group().split("[x+]");
    list.add(Arrays.asList(data));
}
list.forEach(System.out::print);

Prints the output like you expected,

[611, 569, 446, 1200][597, 762, 448, 1101][597, 558, 488, 1207]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=83436&siteId=1