aspose file tools*
The moose likes Java in General and the fly likes Regular Expression for any number Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Regular Expression for any number" Watch "Regular Expression for any number" New topic
Author

Regular Expression for any number

Ninad Kuchekar
Ranch Hand

Joined: Jan 05, 2010
Posts: 64
Hi,

I am trying to use a regular expression to validate numbers.
For e.g.:

-1
+1
1.23456n
123456n.123456n
1,234,567n
1,234,567n.123456n
-1,234,567n.123456n
+1,234,567n.123456n


So its basically any kind of a valid number. [Any number of digits before or after decimal place. The decimal point (may or may not occur) only once throughout and the commas(may or may not occur) any number of times.]

The regular expression that I used is this: ^[-+]?[\\,\\d]*\\.?\\d*$
But it fails for as simple a number as -1 and works for +1
Please help. Any suggestions for new regular expression satisfying the above conditions is also welcome

-Ninad

Don't walk as if you rule the world, walk as if you don't care who rules it...
Stephan King
Greenhorn

Joined: Apr 11, 2010
Posts: 13
For starters this website is handy - I will also have a look whats up with your regex

http://www.regexplanet.com/simple/index.html
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
I think that in the expression [-+], the - is being interpreted as a special character - as in [a-z] or [0-9]. Try escaping it as [\\-+] instead.

Also, here are some sample inputs that I think are likely to be considered numbers, according to your current regex:

12,34,56
,123
123,,456
,,,,,,
,
.
(and last, a blank line, which I can't really show clearly)

It's up to you whether these special cases are important enough to justify further modifying the regex to disallow them.
Stephan King
Greenhorn

Joined: Apr 11, 2010
Posts: 13
^[+-]{0,1}[\d\,]*n?\.{0,1}\d+n?

This seems to work
Ninad Kuchekar
Ranch Hand

Joined: Jan 05, 2010
Posts: 64
Hi,
Thanks to you Mike & Stephan.

Like Mike suggested, I tried escaping the hyphen '-' and it worked perfectly.
I will still like to try with Stephans suggestion...
Thanks anyways you guys!

-Ninad
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Hm, that reminds me of something I forgot to ask earlier. Ninad, why do you have the letter 'n' in so many of your numbers? Since your regex didn't mention them I assumed they were some sort of mistake, but I can't imagine how they came to be. Do users really type "123456n.123456n" instead of "123456.123456"?
Ninad Kuchekar
Ranch Hand

Joined: Jan 05, 2010
Posts: 64
I am so sorry, I should have been clearer.
'n' only means that there could be 'n' number of digits in the number.
It seems, I carried my algebra notations too far.
Thanks again for the help!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regular Expression for any number