tags:

views:

69

answers:

5

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?

+1  A: 

^(?:|\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!

miorel
in your regex the . is mandatory: it doesn't match 25 in the example above
PierrOz
thanks for this mate
Phil P
@PierrOz, yeah I rechecked the question and fixed it :) Thanks!
miorel
you're welcome :)
PierrOz
+4  A: 
^(?:\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
Obalix
thanks for your help
Phil P
@Phil P: If one of the answers solved your problem, upvote and/or accept the best answer.
Obalix
+2  A: 

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})?)?$
soulmerge
+1 for not matching a number ending with a decimal (e.g. "25."). While not explicitly stated in the question, I would assume that requirement.
Mark
A: 
^(\d{1,2}(\.\d{1,6})?)?$
PierrOz
Your regex would match an 8-digit integer.
miorel
@Miorel: ah ha you're right :)I think we gonna have the same one at the end :)
PierrOz
Probably ;) I like how you handled the blank option, I should change mine to that!
miorel
for this collective work you should upvote my answer. I have upvoted yours :)
PierrOz
A: 

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
drewk