user3598397 user3598397 - 21 days ago 6
Java Question

Parsing string in Java

I have a string in java in this format:

5 = cell: 1 [{1.042} {10, 2.387, 2.488} ].


I want to get value 1.042 from this string. But I am not getting regular expression to get that value.

Answer

If the format of the string is:

int=string: int[{float}, {float,float,float}] 
                 ^---^

and you want the bit indicated by "^---^", you don't need regex. Just use String.indexOf to search for occurrences of the [{ and following }:

// The first occurrence of [{, +2 to skip to the following text.
int start = s.indexOf("[{") + 2;

// The first occurrence of } after the [{.
int end = s.indexOf("}", start);

// Extract the substring between these indexes.
String sub = s.substring(start, end);

Ideone demo