Avinash
Avinash

Reputation: 3291

Regular expression accept custom numbers

I need a help

How to change this regular expression that allows to accept positive numbers

like 0, 0.00, .02,etc.. ,

Now this accept 5 digits decimal that is greater than 0 and up to 100

"^100|(\d\d?)(\.(1[01]?|0\d?))?$"

Upvotes: 0

Views: 201

Answers (2)

Tzury Bar Yochay
Tzury Bar Yochay

Reputation: 9004

I am not sure in which language you are wishing to get the result but it seems quite simple. The rules are input should start with either digit(s) or decimal point and then, either decimal point or digit(s).

Upvotes: 0

Gumbo
Gumbo

Reputation: 655219

First you should wrap the whole expression except the string boundaries in a group. Otherwise your expression would just say either start with … or end with … as the | has a higher precedence than ^ and $:

^(100|(\d\d?)(\.(1[01]?|0\d?))?)$

And now a solution to your question:

^((100|[1-9]?[0-9])(\.\d{1,2})?|\.\d{1,2})$

Upvotes: 1

Related Questions