Phil P
Phil P

Reputation: 61

Regular Expression for Decimal or Blank

Sorry for the potentially dumb question but I am trying to pull together a regular expression that will allow:

A number with 1 or 2 numbers before a decimal point, and 0-6 numbers after the decimal point. However I also need to allow the field to be blank if so required.

Valid Examples

0.952321
1.20394
12.12
25
Blank

Invalid Examples

123.45678
1.1234567

Please can anyone help?

Upvotes: 6

Views: 7227

Answers (7)

Roel
Roel

Reputation: 321

I would use one of these:

To match all of your decimals:

(\d+\.\d+){0,1}

To be more specific in number before/after the point, try/variate with any of these:

(\d+\.\d{2}){0,1}
(\d{4}\.\d{2}){0,1}

Upvotes: 0

Ken
Ken

Reputation: 31161

In general, i.e. unlimited decimal places:

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

Upvotes: 0

dawg
dawg

Reputation: 103764

Reading between the lines, I expanded the definition of acceptable input and not and assumed that you only wanted to capture the number in the format described.

For example, these numbers would capture as the number to the right and are all acceptable input:

"0.952321    "  0.952321             (trailing spaces stripped)       
"   1.20394 "   1.20394              (leading and trailing spaces stripped)
"12.12"            12.12             (no leading or trailing spaces)
"12.123   "      12.123
" .1234 "         .1234              (no leading digit -- start with decimal)
"25"                 25              (no decimal) 
" "                " " ?             (space. space captured or not)
"12."               12.              (1 or 2 digits, decimal, no numbers after decimal)

Not OK input:

"."                                   just a decimal
"123456789"                           more than 2 digits lefthand
123                                       ""      "" 
123.45678 
1.1234567                             more than 6 digits right hand
[a-zA_Z]                              not a digit...

So, given that, this regex will do it:

/^\s*(                  # beginning of string and strip leading space                                
 | \d{1,2}\.?           # 1 or 2 digits with optional decimal
 | \d{0,2}\.\d{1,6}     # 0,1, 2 digits with a decimal with digits 
 | \s+                  # remove if you do not want to capture space
  )\s*$                 # trailing blanks to the end
/x

Upvotes: 0

soulmerge
soulmerge

Reputation: 75704

You should provide the language you are using the regular expression in, many have features that will allow you to create more readable expressions. Here is a fail-safe POSIX regex:

^([0-9]{1,2}\.[0-9]{0,6})?$

If the decimal part is optional, you can use

^([0-9]{1,2}(\.[0-9]{1,6})?)?$

Upvotes: 2

pierroz
pierroz

Reputation: 7870

^(\d{1,2}(\.\d{1,6})?)?$

Upvotes: 0

AxelEckenberger
AxelEckenberger

Reputation: 16926

^(?:\d{1,2}(?:\.\d{0,6})?)?$

Should do the trick.

\d     matches any digit
{n,m} specifies the number of occurrences
(?: ) creates an anonymous group
^     specifies the start of the string
$               the end of the string
?     means the group is optional

Upvotes: 12

miorel
miorel

Reputation: 1833

^(?:|\d{1,2}(?:\.\d{0,6})?)$

The part before the pipe matches blank. The part after matches one or two digits optionally followed by a period and up to six digits. The ?: are so we don't use capturing groups unless needed.

Hope this helps!

Upvotes: 2

Related Questions