Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression for float and percentage

Tags:

c#

regex

I need regex patterns for floating point numbers with optional % sign at the end like

12.32
12.32%
0.32
.32
.32%

arbitrary length of numbers on left and right of floating point numbers. I need this to validate input in asp.net mvc app
UPDATE:
forgot following combinations

12%
35
45%
like image 409
Muhammad Adeel Zahid Avatar asked May 12 '26 22:05

Muhammad Adeel Zahid


1 Answers

This regex should do it for you...

\d*\.\d+%?

Which means... zero or more digits (\d*) followed by a period (escaped \.) followed by one or more digits (\d+) followed by an optional % (%?)


Update: match whole numbers

\d*\.?\d+%?

regex diagram

like image 103
Billy Moon Avatar answered May 15 '26 12:05

Billy Moon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!