problem with a regular expression

  • Thread starter Thread starter giulio santorini
  • Start date Start date
G

giulio santorini

Hi,
I would like to build a regular expression that allows
only a number with 3 integer the decimals separator and
other three decimals.
I've build the firt part in this way:
^([1-9]{1}|[1-9]{1}[0-9]{1}|100)$

Now I'am tring to build the second one but... it's quite
difficult.
Somebody could help me? This is what I've done:
^([1-9]{1}|[1-9]{1}[0-9]{1}|100)\.([0-9]{1}|[0-9]{2}|[0-9]
{3})$

But it doesn't work as I would... :-\
Thanks in advance!
 
giulio santorini said:
Now I'am tring to build the second one but... it's quite
difficult.
Somebody could help me? This is what I've done:
^([1-9]{1}|[1-9]{1}[0-9]{1}|100)\.([0-9]{1}|[0-9]{2}|[0-9]
{3})$

But it doesn't work as I would... :-\


Tell us what you expect to happen, what actually happens, and why you
don't think that's right.

Give some examples of strings on which it should work but doesn't.
Give some examples of strings on which it shouldn't work but does.




All that aside, are you escaping the escape character--so that C#
doesn't digest your \ ? That is, you have the following, right?

^([1-9]{1}|[1-9]{1}[0-9]{1}|100)\\.( etc. )$

That is, \\. instead of \.


By the way, the {1} quantifiers are redundant. You could probably get
away with the following:

Regex r = new Regex("^([1-9][0-9]?|100)\\.([0-9]{1,3})$");


That regex looks for:


* the start of the string

then either

* one digit between 1 and 9
* and, optionally, one digit between 0 and 9

or

* the string 100

* a decimal point

* between one and three digits, each between 0 and 9

* the end of the string
 
Back
Top