Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,

I want to write Regular Expression for below formats.

Note - It should not accept 0 or alpha-bates.

Quote:
8.5
8.52
100000
2000000
0.85


What I have tried:

I have tried so many combination. but no luck.

Quote:
(?
Posted
Updated 21-Apr-22 0:20am

Simple:
RegEx
^[-+]?\d+(\.\d+)?$
.NET Regular Expressions | Microsoft Docs[^]

But depending on your requirements, it would probably be simpler to use decimal.TryParse to test whether the string is a valid number.

Edit: Excluding zero is slightly more complicated - a zero-width positive lookahead assertion is probably the simplest option:
RegEx
^[-+]?(?=.*[1-9])\d+(\.\d+)?$
Grouping Constructs in Regular Expressions | Microsoft Docs[^]

Again, if your intention is to validate a numeric input, then it would be better to use decimal.TryParse combined with any range checks on the resulting number.
 
Share this answer
 
v2
Comments
CPallini 21-Apr-22 7:45am    
5.
Maciej Los 21-Apr-22 10:59am    
5ed!
Member 11052432 22-Apr-22 3:12am    
Hi Thanks for the solution.

However it's accepting 0 & 0.00 as well like below format.

0 - should not accept
0.00 - should not accept

0.85 - should accept
anything > 0 - should accept
8.5 - should accept
8.52 - should accept
100000 - should accept
2000000 - should accept
0.85 - should accept

Can you please provide regular expression for above formats?

Thanks in advance.
Quote:
I have tried so many combination. but no luck.

It should not be a matter of luck, but a matter of learning and practicing.

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
 
Comments
CPallini 21-Apr-22 7:45am    
5.
Patrice T 21-Apr-22 8:00am    
Thank you
Maciej Los 21-Apr-22 10:59am    
5ed!
Patrice T 21-Apr-22 14:59pm    
Thank you

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