Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can any one please help me with the regular expression for any number from 0 to 100

Valid Number

0
0.00
12.123456
100.00
100

Invalid Number

-10.12
100.11
101

I referred the below link and modified the RegX value like
"^(100|\\d)(\\.\\d{1,8})?$";
for my requirement. But it is not worked out for me. Also, the result is not accurate.

Regex expression for 0-10 numbers with two decimal points[^]

What I have tried:

I tried this RegX .
"^(100|\\d)(\\.\\d{1,8})?$";
But, not worked for me
Posted
Updated 28-Jun-22 8:43am
Comments
PIEBALDconsult 28-Jun-22 9:42am    
Untested, and not fully awake yet -- ^((100)|(\d{1,2}(\.\d{1,8})?))$
CHill60 28-Jun-22 10:01am    
Tested for you - it works. Definitely a solution to post
CHill60 28-Jun-22 10:03am    
Looking at the way you have just thrown some extra \ characters in there, you might find this link useful Regex Cheat Sheet[^]

Piebald's solution:
((100)|(\d{1,2}(\.\d{1,8})?))
Isn't quite right - it doesn't detect "100.00" as a valid number, and on some systems it assumes that (100) is a numbered capture group.
Try this:
^(?<whole>100|\d{1,2})(?<fract>\.\d{1,8})?$


If you want to work with regexes, get a copy of Expresso[^] - it's free, and it examines and generates Regular expressions.
 
Share this answer
 
v2
Comments
CHill60 28-Jun-22 11:09am    
Good spot - I missed that test case when I checked it and suggested Piebald posted it
OriginalGriff 28-Jun-22 11:21am    
Easy to miss it! That's half the problem with testing regexes ... :sigh:
Udhaya Bharathi 28-Jun-22 12:01pm    
Thanks for your solution. I am using this RegX while entering value in TextBox. But If I type value 10. then this condition is failed. So, I can't enter decimal values.

TextBox txtBox = (TextBox)sender;
string regx = "^(?<whole>100|\\d{1,2})(?<fract>\\.\\d{1,8})?$";
if (!Regex.IsMatch(txtBox.Text.Trim(), regx))
{
//Throws error
}
If you have any idea on this, please share
Udhaya Bharathi 28-Jun-22 12:06pm    
Result:
Regex.IsMatch("10.5", regx)
true
Regex.IsMatch("10.", regx)
false // <= It should be true
Regex.IsMatch("101", regx)
false
Regex.IsMatch("100", regx)
true
OriginalGriff 28-Jun-22 14:09pm    
You need to learn how regexes work if you are going to use them: none of your examples end with a period, so it wasn't considered as valid by either Piebald or myself.
To allow it, you need to reduce the minimum number of digits allowed after the period from one to zero. If you have understood the examples we gave you, you should be able to change that very easily.
I would try something like : ^100(\.0{1,8})?|\d{1,2}(\.\d{1,8})?$
Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx: Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx: Regexper[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900