• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

Check against Numbers and dot in a string

Using Regular expressions . I want to check a string. The string must contain only numbers and one dot.

Example Valid Values:
909
909.99

Example Invalid Values:
abcd
a_b
9abcd
909.909.909
909.
9*

How can I do this?

Thanks
0
praveenuni
Asked:
praveenuni
  • 3
  • 2
1 Solution
 
CeiledCommented:
If you're trying to parse a decimal number, I would recommend using Double.Parse() or Double.TryParse() instead, as they'll handle this task in a much more generic, extensible and globalization-friendly manner than anything you could do with a regular expression.

If you want to parse this exact syntax for some other reason, this regular expression should do the trick:

\d+(?:\.\d+)?

That expression assumes you want to require numbers on the left of the period, while the period itself and the numbers after it are optional. You can change the "+" symbols to "*" to make the banks of numbers optional (for example, changing the second + to a * would  allow the period to appear with no numbers after it). Let me know if this isn't quite what you need.
0
 
praveenuniAuthor Commented:
The above regular expression is not checking for numbers + alphabets. for eg: 9abc2
If I enter the above string its not showing as an invalid string.
0
 
praveenuniAuthor Commented:
and its not showing error for eg., 909.909.909

Any suggestions
0
 
CeiledCommented:
Ah, sorry...you want to make sure the entire string consists of nothing but a match to this pattern. In that case, you just need to place start and end anchors around it, like so:

^\d+(?:\.\d+)?$
0
 
praveenuniAuthor Commented:
Great Ceiled .. That worked ..

Thanks
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now