I am trying to parse doubles from a line of text. I found few lines of code that I thought was doing what I wanted. However, I discovered it would not parse negative doubles (would just parse them as positive). I am totally new to regex and its a little overwhelming. I was wonder if someone could explain the code below and tell me what each part means, and how I would modify it so it will parse both positive and negative doubles? Thanks in advance!
double nextDouble;
Matcher matcher = Pattern.compile("(?!=\\d\\.\\d\\.)([\\d.]+)").matcher(line);
if (matcher.find()) nextDouble = Double.parseDouble(matcher.group(1)));
EDIT: Here is an example of a line.
“104.44518717, 34.09785265, 103.39288764, 0.09813121, 0.00000000, -0.46612322, 4.68576504”
It parses -0.46612322 as 0.46612322.
The expression you have has two parts.
The fist part in the brackets is ensuring that the matching string isn’t preceded by digits
and points. Not sure why you need that part, if you provide a sample line that would help.
The second part in the brakets is the matching part. That just says match 1 or more digits and points.
If you want to make your expression to work with negative numbers you can just add the ‘-‘ character to the matching set.
But this, as well as your expression will match strings with multiple points(dots). Eg 122.12.12
So I suggest you try the following. Note that i removed the first part.