2

Examples:

"1"     yes
"-1"    yes
"- 3"   no
"1.2"   yes
"1.2.3" no
"7e4"   no  (though in some cases you may want to allow scientific notation)
".123"  yes
"123."  yes
"."     no
"-.5"   yes
"007"   yes
"00"    yes
dreeves
  • 26,430
  • 45
  • 154
  • 229

5 Answers5

4

This allows for optional "+" and "-" in front. And allows trailing or initial whitespace.

/^\s*[+-]?(?:\d+\.?\d*|\d*\.\d+)\s*$/
dreeves
  • 26,430
  • 45
  • 154
  • 229
4

There are several alternatives. First, using a zero-width look-ahead assertion allows you to make the rest of the regex simpler:

/^[-+]?(?=\.?\d)\d*(?:\.\d*)?$/

If you want to avoid the look-ahead, then I'd try to discourage the regex from back-tracking:

/^[-+]?(?:\.\d+|\d+(?:\.\d*)?)$/
/^[-+]?(\.\d+|\d+(\.\d*)?)$/ # if you don't mind capturing parens

Note that you said "base 10" so you might actually want to disallow extra leading zeros since "014" might be meant to be octal:

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

Finally, you might want to replace \d with [0-9] since some regexes don't support \d or because some regexes allow \d to match Unicode "digits" other than 0..9 such as "ARABIC-INDIC DIGIT"s.

/^[-+]?(?:\.[0-9]+|(?:0|[1-9][0-9]*)(?:\.[0-9]*)?)$/
/^[-+]?(\.[0-9]+|(0|[1-9][0-9]*)(\.[0-9]*)?)$/
tye
  • 1,157
  • 9
  • 11
2

Matches all specified examples, doesn't capture any groups:

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


To not match "1." (etc):

^[+-]?(?:\d+(?:\.\d+)?|\.\d+)$


Doesn't bother with whitespace (use a trim function).

Peter Boughton
  • 110,170
  • 32
  • 120
  • 176
2

The regular expression in perlfaq4: How do I determine whether a scalar is a number/whole/integer/float? does what you want, and it handles the "e" notation too.

while( <DATA> )
    {
    chomp;

    print /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ ?
        "$_ Works\n"
        :
        "$_ Fails\n"
        ;
    }

__DATA__
1
-1
- 3
1.2
1.2.3
7e4
.123
123.
.
-.5
brian d foy
  • 129,424
  • 31
  • 207
  • 592
0

Depending on the language you are coding in this functionality may already exist.

EBGreen
  • 36,735
  • 12
  • 65
  • 85