Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regular Expression for any number

 
Ninad Kuchekar
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Stephan King
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3036
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
^[+-]{0,1}[\d\,]*n?\.{0,1}\d+n?

This seems to work
 
Ninad Kuchekar
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3036
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic