Sri
Sri

Reputation: 1217

regular expression to match exactly 0.[001 to 999]

I tried (^[0]?)\.*(?=.*[1-9])\d{1,3}?$expression to match input value which should only accept 0.[001 to 999] , now the problem is :

it is matching 012 or 090 etc numbers too.

I wanted the expression to match exactly 0.[001 - 999]

Any help is appreciated.

Thanks, Sri

Upvotes: 1

Views: 3358

Answers (2)

Sri
Sri

Reputation: 1217

I am accepting @Crazysheep answer. Because that * caused the issue.

(^[0]?)\.(?=.*[1-9])\d{1,3}?$ seems to be working.

Tested at https://www.regex101.com/

Thank you all for quick response.

Upvotes: 0

cbreezier
cbreezier

Reputation: 1304

You had \.* which matches 0 or more. It matches 0, therefore you get to match stuff like 012.

^0\.[0-9]{2}[1-9]$

Matches a 0, then a ., then [0-9] twice, then [1-9]

Edit: Jonathan is right, this doesn't properly match stuff like 0.010.

^0\.[0-9]{3}$

and then ensure that it is not 0.000 would work.

Alternatively try this ugly one: ^0\.(?:[0-9]{2}[1-9]|[0-9][1-9][0-9]|[1-9][0-9]{2})$

Upvotes: 3

Related Questions