简体   繁体   中英

How to set up a Regex for 000.00000001%-100.00000000%

I have a RegularExpressionValidator in some .net code that should only allow 000.00000001%-100.00000000%. I have the following, but then realized that it allows for all forms of '0'.

(^0?[0-9]?[0-9]?)(.\\d{0,8})?\\s?\\%?$|(^100(.[0]{0,8})?\\s?\\%?$)

(It is intentional to allow for a potential ' %' or '%' at the end.)

I want to prevent the entry of all entries that equate to '0' such as:

0

00

000

000.0

000.00

~

.00000000

I appreciate any help provided. Thanks!

I'll use your regex:

(^0?[0-9]?[0-9]?)(.\d{0,8})?\s?\%?$|(^100(.[0]{0,8})?\s?\%?$)

Simplify it a bit:

^0?\d{0,2}(.\d{0,8})?\s?%?$|^100(.0{0,8})?\s?%?$

Fix the dots:

^0?\d{0,2}(\.\d{0,8})?\s?%?$|^100(\.0{0,8})?\s?%?$

Reorder it:

^(0?\d{0,2}(\.\d{0,8})?|100(\.0{0,8})?)\s?%?$

Construct the following lookahead to ensure you are not matching something zeroish :

(?!0*\.?0*\s?%?$)

And prepend it to the regex:

(?!0*\.?0*\s?%?$)^(0?\d{0,2}(\.\d{0,8})?|100(\.0{0,8})?)\s?%?$

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM