Hi there everyone,

I am having some difficulties getting a regular expression to work. I have a line of text with 5 numbers.I want to ignore the first number, and grab the other 4 numbers. Here is what the text I am dealing with looks like.

1681    12.33754513 7.066246057 6.079261254 2.953020134
1171    9.676875527 12.76832461 2.725877787 9.275756545

Here is the regular expression I came up with(I'm a noob at regexs).

Pattern p = Pattern.compile("\\D+(\\d+)(\\d+)(\\d+)(\\d+)");
Matcher m = p.matcher(lineString2);
if(m.find()){
    double nr1 = Double.parseDouble(m.group(1));
    double nr2 = Double.parseDouble(m.group(2));
    double nr3 = Double.parseDouble(m.group(3));
    double nr4 = Double.parseDouble(m.group(4));

Can anyone help me out with this? I really appreciate any and all thoughts/comments/suggestions.

Bonus Points: If anyone could suggest a way that if a line contains N/A or NaN I could just make that value 0 and still grab the actual numbers aswell.

1838    73.32185886 100 80.13204361 N/A
1298    86.31226455  NaN    N/A N/A

The character class \D means anything that is not a digit 0 through 9, just like [^0-9]. I can't guess what you intend with \\D+ at the start of your expression.

You have forgotten to allow for spaces between your numbers and dots inside your numbers. You could represent numbers in your expression something like \\d+\\.\\d+ but it seems easier to just use (.+) and rely on the spaces in your input to separate the numbers, as in:

.+ (.+) (.+) (.+) (.+)

That will also match your input if some of the numbers are N/A or NaN, so you can easily test for those cases before your parsing step.

Thanks a lot bguild, I was able to grab the information I needed. Now I am stuck trying to convert my NaN's and N/A's to a 0 so I can make them a double. I tried using an if(variable =="NaN"){variable = "0"} but for whatever reason the if statement is not being executed. I am just going to post a new thread for this question though since it strays pretty far from regexs I think. Thanks again!

This question has already been answered. Start a new discussion instead.