2

I would like to know how to parse several double numbers from a string, but string can be mixed, for instance: String s = "text 3.454 sometext5.567568more_text".

The standard method (Double.parseDouble) is unsuitable. I've tried to parse it using the isDigit method, but how to parse other characters and .?

thanks.

4 Answers 4

7

You could search for the following regex:

Pattern.compile("[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?")

and then use Double.parseDouble() on each match.

Sign up to request clarification or add additional context in comments.

4 Comments

you can also use \\d instead of [0-9]
Here's another good link should you choose to use a regex: regular-expressions.info/floatingpoint.html
Regex is the way to go if you can spare a few cpu clock cycles.
it doesn't correctly parses the doubles. for instance, if i have a String like "sdf9.99e.23" it parses 9.99 or as i modified it, it gives 9.99e23, BUT! it should throw some exception or return false(or something other,but not a double value), it shouldn't return the double value in a such case
3

After parsing your doubles with the suitable regular expressions like in this code or in other posts, iterate to add the matching ones to a list. Here you have myDoubles ready to use anywhere else in your code.

public static void main ( String args[] )
{
    String input = "text 3.454 sometext5.567568more_text";
    ArrayList < Double > myDoubles = new ArrayList < Double >();
    Matcher matcher = Pattern.compile( "[-+]?\\d*\\.?\\d+([eE][-+]?\\d+)?" ).matcher( input );

    while ( matcher.find() )
    {
        double element = Double.parseDouble( matcher.group() );
        myDoubles.add( element );
    }

    for ( double element: myDoubles )
        System.out.println( element );
}

10 Comments

i think it's just what i wanted!
it doesn't correctly parses the doubles. for instance, if i have a String like "sdf9.99e.23" it parses 9.99 or as i modified it, it gives 9.99e23, BUT! it should throw some exception or return false, it shouldn't return the double value in a such case
because 9.99e is a double as you know "e" is used for scientific notation of doubles, if you don't want to parse them remove [eE] from regex in the code.
@Helgus Input "sdf9.99e.23" contains 2 doubles: 9.99 and 0.23, you will see these match the regex and will be the output. my answer is valid for your original question and it doesn't specify the thing you are telling now. you should ask it in another question. by the way the regex in my answer is the general double regex.
@Helgus: yes, I understand that. But an X for example isn't valid after a digit in a double number anyway and when the input ist 123X you simply ignore it and say that the input contains 123. What is the rule that makes you ignore the X but not the e...
|
1
  1. Parse out the substring (surrounded by whitespace)

  2. Use String.ParseDouble() to get the numeric value

Here's one example, using "split()" to parse (there are many alternatives):

// http://pages.cs.wisc.edu/~hasti/cs302/examples/Parsing/parseString.html

String phrase = "the music made   it   hard      to        concentrate";
String delims = "[ ]+";
String[] tokens = phrase.split(delims);

Here's a second alternative:

Java: how to parse double from regex

Comments

0

You'll need to think about what sort of algorithm you'd want to use to do this, as it's not entirely obvious. If a substring is asdf.1asdf, should that be parsed as the decimal value 0.1 or simply 1?

Also, can some of the embedded numbers be negative? If not this greatly simplifies the search space.

I think that aix is on the right track with using a regex, since once you come up with an algorithm this sounds like the kind of job for a state machine (scan through the input until you find a digit or optionally a - or ., then look for the next "illegal" character and parse the substring normally).

It's the edge cases that you have to think about though - for example, without negative numbers you can almost use s.split("[^0-9.]") and filter out the non-empty elements. However, period characters that aren't part of a number will get you. Whatever solution you go with, think about whether any situations could trip it up.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.