Regular expression for 1 decimal

  • Thread starter Thread starter bas jaburg
  • Start date Start date
B

bas jaburg

Hi,

I need to know the regular expression that checks the whether a
1-decimal number has been entered. Also it needs to ok if no trailing
number before the decimal dot has been entered

So:
1.3 --> ok
1.0 --> ok
.1 --> ok
11.1 --> ok

1.11 --> not ok

I have this one [0-9]*\.?[0-9] but it does not do the .1 test
properly.

Thanks in advance.

Bas Jaburg
www.jaburg.com
 
Hello bas,

If I understand you correctly you want a regular expression that will determine if a number has only one digit passed the decimal point.

What you have is close but it will also accept 1.11.

Try
^[0-9]*\.[0-9]$

This the ^ and $ force it to match the entire string.

HTH
Wes Haggard
http://weblogs.asp.net/whaggard/
 
shouldn't the * be in the front?

*[0-9]\.[0-9]

meaning that there can be any number of values from 0-9, with none included,
then a ".", then one digit.

? - 1 or more
* - 0 or more
 
Doh, go with what wes said, it's been a few months since i looked at this.
;o)


Dan =o) said:
shouldn't the * be in the front?

*[0-9]\.[0-9]

meaning that there can be any number of values from 0-9, with none
included, then a ".", then one digit.

? - 1 or more
* - 0 or more


bas jaburg said:
Hi,

I need to know the regular expression that checks the whether a
1-decimal number has been entered. Also it needs to ok if no trailing
number before the decimal dot has been entered

So:
1.3 --> ok
1.0 --> ok
.1 --> ok
11.1 --> ok

1.11 --> not ok

I have this one [0-9]*\.?[0-9] but it does not do the .1 test
properly.

Thanks in advance.

Bas Jaburg
www.jaburg.com
 
Hi bas! :O)

try this pattern :
//***
bool ret = Regex.IsMatch(".4", @"^\d*\.\d+?$");
//***

or this (more permissive) :
//***
string number = ".4";
bool ret = false;
double d = 0;
ret = double.TryParse(number, NumberStyles.Number, out d);
//***
 
bas said:
I need to know the regular expression that checks the whether a
1-decimal number has been entered. Also it needs to ok if no trailing
number before the decimal dot has been entered

So:
1.3 --> ok
1.0 --> ok
.1 --> ok
11.1 --> ok

1.11 --> not ok

Use the "exactly <n>" quantifier and the word boundary delimiter:

Regex regex = new Regex(@"(\d*\.\d{1}\b)",
(RegexOptions) 0);

(Note that this will also capture the decimal part if your input string
is something like "abc.8".)
 
Back
Top