0

I want to check if a user input is a valid double number (X.XXXXX).

This is my code:

if(textField.getText().matches("^[0-9][.]+[0-9]$"))
{
    System.out.println("This is a double");
}

If the user inputs 5.5, println gets executed. If user inputs 5.55 or a number with more numbers after the dot (5.5XXX...) it will not match.

How can I define a regex that matches all numbers after the dot, no matter how many?

jwpfox
  • 5,124
  • 11
  • 45
  • 42
Suroko
  • 103
  • 1
  • 11
  • 2
    Why don't you juse use `Double#parseDouble` and check for a `NumberFormatException`? – fge Sep 13 '16 at 10:47
  • `"[0-9]+[.][0-9]+"` - you do not need the anchors and you need to allow 1 or more digits with `+` quantifier. – Wiktor Stribiżew Sep 13 '16 at 10:47
  • Possible duplicate of http://stackoverflow.com/questions/3681242/java-how-to-parse-double-from-regex – uoyilmaz Sep 13 '16 at 10:47
  • 2
    You may want to [see some relevant documentation](https://msdn.microsoft.com/en-us/library/az24scfc(v=vs.110).aspx) for basic regex, here specifically the 'Quantifiers' section. – Thernys Sep 13 '16 at 10:48

3 Answers3

2

Your plus + is misplaced:

^[0-9][.]+[0-9]$

lets users enter

5......1

You need to move it to the end:

[0-9][.][0-9]+

Anchor tags are not necessary because you are using matches().

Note that this regex is overly simplistic, because it does not support negative numbers, forces users to enter .0 for whole numbers, and does not allow scientific notation, among other things. A regex that supports these features is much more complex (see this Q&A for an example). You would be better off using a built-in parsing method to check if the input is valid or not.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • The anchors are redundant since OP is using `matches()`. – Wiktor Stribiżew Sep 13 '16 at 10:49
  • @DavidDvorak: You [said](http://stackoverflow.com/questions/39468145/match-number-with-unlimited-decimal-places/39468192#comment66256703_39468271) *So i want that the textfield accepts numbers like: 5 | 1.2 | 1.234 | 1234.13156 | etc.* - no idea why that works for you. – Wiktor Stribiżew Sep 13 '16 at 11:21
  • @WiktorStribiżew I don't think his modified expression works, so my best guess is that he took a more complete expression from the Q&A link. – Sergey Kalinichenko Sep 13 '16 at 11:49
0

This is your regex:

[+-]([0-9]*[.])?[0-9]+

This will accept

  • 123
  • 456.789
  • .555 and so on..
Martin Gardener
  • 1,001
  • 8
  • 14
0

Others have already mentioned regex solutions (Danyal's seems the most comprehensive so far). The "real" regex for a double is quite complicated! See this question here:

Related question on SO

But I think you are asking the wrong question (none still addresses scientific notation though):

What you want to achieve is answering "Is this string a double?". Now there are things that are far more readable and overall "better" than a regex for that.

For example, as mentioned in the comments, you can actually try to parse the string and see if it fails to be parsed. This is also the recommended approach in the question I linked, and the one you should follow.

Community
  • 1
  • 1
Diego Martinoia
  • 4,592
  • 1
  • 17
  • 36
  • I wanted to keep the qustion simple. What im overall trying to achieve: User inputs vlaue into a textfield. This value is then going to be converted to a double because i need it for some math stuff. So i want that the textfield accepts numbers like: 5 | 1.2 | 1.234 | 1234.13156 | etc. – Suroko Sep 13 '16 at 11:03
  • @DavidDvorak: Does that mean you need to validate numbers separated with `space`+`|`+`space`s? Please add all relevant details to the question itself. – Wiktor Stribiżew Sep 13 '16 at 11:08
  • Sorry for the confusion, the | were just meant to be separators. @dasblinkenlight answer solved my problem. However also thanks to you for trying to help me. To simplify the problem. User inputs a number. The programm checks if it matches my defined regex and then assigns this to a double variable. If it doesnt match my regex (for example it has a , inside, or the user inputs characters, it shows a warning. – Suroko Sep 13 '16 at 11:21
  • @DavidDvorak if you are 100% sure that just "normal" numbers will appear than that regex is fine. But doubles take many forms (1e5, 10,000,000.47, NaN...) so think about those too. – Diego Martinoia Sep 13 '16 at 12:24