Regular expression for any number greater than 0?

Applying validation for a model in MVC and using it with Regex.

To verify that my model ID is greater than 0, submit.

I am not familiar with Regex ... can someone help me?

thank

+46
regex asp.net-mvc-3 model
Jan 27 '12 at 18:53
source share
13 answers

I don't know how MVC matters, but if your identifier is an integer, this BRE should do:

^[1-9][0-9]*$ 

If you want to match real numbers (float), not integers, you need to handle the above case, as well as regular decimal numbers (i.e. 2.5 or 3.3Μ… ), cases when your pattern is between 0 and 1 (i.e. e. 0.25 ), as well as the case when your template has a decimal part equal to 0. (i.e. 2.0 ). And while we are on it, we will add support for leading zeros to integers (i.e. 005 ):

  ^(0*[1-9][0-9]*(\.[0-9]+)?|0+\.[0-9]*[1-9][0-9]*)$ 

Please note that this second is an extended RE. The same can be expressed in Basic RE, but almost everyone understands ERE these days. Let me break down the expression into parts that are easier to assimilate.

  ^( 

The carriage matches the zero value at the beginning of the line, so before your regular expression with a carriage, it binds it to the beginning of the line. The opening bracket is due to the or-bar below. More on this later.

  0*[1-9][0-9]*(\.[0-9]+)? 

This matches any integer or any floating point number higher than 1. Thus, our 2.0 will match, but 0.25 won't. 0* at the beginning processes leading zeros, therefore 005 == 5 .

  | 

In this context, the pipe symbol is "or-bar". To evaluate this expression, it takes precedence over everything else and effectively combines the two regular expressions together. Parentheses are used to group multiple expressions separated by or-bars.

And the second part:

  0+\.[0-9]*[1-9][0-9]* 

This matches any number starting with one or more characters 0 (replace + by * with zero or more zeros, i.e. .25 ), followed by a period, followed by a string of digits that includes at least one that does not is 0 . Thus, this corresponds to all above 0 and below 1 .

  )$ 

And finally, we close the parentheses and attach the regular expression to the end of the line with a dollar sign, just like a carriage is attached to the beginning of the line.

Of course, if you let your programming language evaluate something numerically, rather than try to match it with a regular expression, you will save headaches and a processor.

+101
Jan 27 '12 at 18:56
source share

How about this: ^[1-9][0-9]*$

+11
Jan 27 '12 at 18:56
source share
+3
Mar 30 '15 at 20:15
source share

there you go:

 MatchCollection myMatches = Regex.Matches(yourstring, @"[1-9][0-9]*"); 

on submit:

 if(myMatches.Count > 0) { //do whatever you want } 
+1
Jan 27 '12 at 18:56
source share

I tried this and it worked for me for all decimal / integer numbers greater than zero

Allows a space: ^\s*(?=.*[1-9])\d*(?:\.\d{1,2})?\s*$

No space: ^(?=.*[1-9])\d*(?:\.\d{1,2})?$

Reference: Regex is greater than zero with 2 decimal places

+1
May 19 '13 at
source share

If you only need non-negative integers, try: ^\d+$

+1
Nov 23 '14 at 19:03
source share

I think the best solution is to add a + sign between the two brackets of the regex expression:

 ^[1-9]+[0-9]*$ 
+1
May 14 '17 at 2:47 p.m.
source share

You can use the following expression:

 (^\d*\.?\d*[1-9]+\d*$)|(^[1-9]+\.?\d*$) 

Allowed values: 1 1. 1.1 1.0 all positive real numbers

Invalid entries: all negative real numbers and 0 and 0.0

0
Apr 25 '14 at 7:15
source share

Simple answer: ^[1-9][0-9]*$

0
Sep 14 '17 at 10:18
source share

I think this works fine:

 ([1-9][0-9]*(\.[0-9]*[1-9])?|0\.[0-9]*[1-9]) 

Valid:

  1 1.2 1.02 0.1 0.02 

Invalid:

 0 01 01.2 1.10 
-2
Oct 21 '14 at 23:19
source share
 [1-9]\.\d{1,2}|0\.((0?[1-9])|([1-9]0?)){1,2}\b 
-2
May 21 '16 at 19:27
source share

^ \ $? ([1-9] {1} [0-9] {0.2} (\, [0-9] {3}) * (. [0-9] {0.2}) |? [1- 9] {1} [0-9] {0} ([0-9] {0.2}) |.?. 0 ([0-9] {0.2})? | (. [0-9 ] {1,2})?) $

-four
Jan 23 '13 at 6:59
source share

A very simple answer to this question: \ d *

-6
Oct. 14 '12 at 15:25
source share



All Articles