• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Regular Expression for any number

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Master Rancher
Posts: 5122
82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Master Rancher
Posts: 5122
82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
What are you doing? You are supposed to be reading this tiny ad!
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic