Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression almost perfect for a Numeric Value

Tags:

c#

regex

I have this REGEX almost perfect ... It seems to handle everything except a number that leads with a negative sign and then a decimal. So if I enter:

-.2

I get an error -

Here is my Regex -- everything else I've tested works perfectly...

^(\+|-)?[0-9]{1,11}?(?:\.[0-9]{1,4})?$

This allows for:

  • a number up to 11 digits (99 Billion)
  • positive or negative number
  • up to 4 decimal places (optional)
  • leading 0 before decimal point is optional - for positive numbers only

These all work:

-0.2345
-10
12
.125
0.1245
5.555
25000000000 (aka 25 Billion)
25000000000.25 

These do not work:

-.2
-.421
like image 699
Danimal111 Avatar asked Apr 28 '14 20:04

Danimal111


People also ask

What is the regular expression for numbers only?

To check for all numbers in a field To get a string contains only numbers (0-9) we use a regular expression (/^[0-9]+$/) which allows only numbers. Next, the match() method of the string object is used to match the said regular expression against the input value.

How do you find the range of a number in regex?

To show a range of characters, use square backets and separate the starting character from the ending character with a hyphen. For example, [0-9] matches any digit. Several ranges can be put inside square brackets. For example, [A-CX-Z] matches 'A' or 'B' or 'C' or 'X' or 'Y' or 'Z'.

What is the regular expression used if you want to only allow for digits 1 through 9?

The regex [0-9] matches single-digit numbers 0 to 9. [1-9][0-9] matches double-digit numbers 10 to 99.

What is a valid regular expression?

What is RegEx Validation (Regular Expression)? RegEx validation is essentially a syntax check which makes it possible to see whether an email address is spelled correctly, has no spaces, commas, and all the @s, dots and domain extensions are in the right place.


2 Answers

Regex can be expensive... Why not use Decimal.Parse or Float.parse?

Your current implementation would never work with alternate number styles, like European where . (dot) and , (comma) are interchanged ...whereas Decimal.Parse will:

string stringValue = "45.889,33";
CultureInfo currentCulture = Thread.CurrentCulture; //set this way up in the execution chain
decimal thenumber = Decimal.Parse(stringValue, currentCulture);
//thenumber = 45889.33 in us-en display format.

Numerical parsing is not a good application for regex, IMO.

like image 111
enorl76 Avatar answered Oct 04 '22 03:10

enorl76


Try this:

^(\+|-)?[0-9]{0,11}?(?:\.[0-9]{1,4})?$

Update:

The above regex accepts strings +, - and (the empty string). You can use a lookahead to restrict those. The lookahead ensures there must be a character after the + or - sign.

The correct solution is:

^(\+|-)?(?=.{1})[0-9]{0,11}(?:\.[0-9]{1,4})?$

A working example:

Strings accepted:

-0.2345
-10
12
.125
0.1245
5.555
-.2
-.421

Strings not accepted:

100000000000.0001
+
-

123456789012
1111111111.12345
+1.11.1
-2.
like image 28
John Bupit Avatar answered Oct 04 '22 03:10

John Bupit