22

I have got this regexp "^[0-9]+\.?[0-9]*$") to match a double number or an integer number in visual C++ but it doesn't seem to work. Any ideas?

This is how I am applying the code:

if (System::Text::RegularExpressions::Regex::IsMatch(e0, "^[0-9]+\.?[0-9]*$")){
     e0_val = System::Convert::ToDouble(e0);
}
7
  • 2
    Why do you think it doesn't work? Give an example. May 9, 2012 at 13:20
  • @AndrewLogvinov +1, "doesn't work gimme teh codez" is not a good question form.
    – user529758
    May 9, 2012 at 13:21
  • Whitespace at the start or end perhaps? Can you post the code that's using it? May 9, 2012 at 13:22
  • when I try to use it to validate a number input in a textbox it returns a message indicating that the number 0.16 is not a number
    – cobie
    May 9, 2012 at 13:22
  • 2
    Are you using C++/CLI? If so, why don't you use Double::TryParse instead?
    – Mithrandir
    May 9, 2012 at 13:28

7 Answers 7

22

the regexp above is not perfect since it accepts "09" which is not a valid number. a better expression would be:

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

where:

1. is an optional negative sign;
2. is zero or a valid non-zero integer;
4. is the optional fracture part;

in theory, the fracture part should be written as "(\.[0-9]*[1-9])?" instead, because a number must not have tailing zeroes. in practice, the source string might have been created with a fixed number of digits e.g:

printf("%.1f", x);

so it might easily end with a zero character. and, of course, these are all fixed point representations, not the doubles themselves. a double number can be written as -1.23e-4 as well instead of -0.000123.

3
  • 09 is a valid number and trailing zeros are allowed. Eg 9.45000 is a valid number.
    – dannyhut
    May 21, 2023 at 22:33
  • @dannyhut: it depends. usually 09 and 08 are not valid decimal numbers. a zero prefix means octal notation, and allowed digits after the prefix are 0-7 only.
    – ThatsMe
    May 31, 2023 at 10:10
  • It is perfectly acceptable to place 0's in front of a decimal number. We mostly don't because its unnecessary but it is fine. 09 is a 0 is the ten's column and a 9 in the one's column. This also applies to 0 after the numbers in the exponent of a number. Even many programming languages not just English have no problems with this. Examples include dates, phone numbers, prices in menus and anywhere padding is needed.
    – dannyhut
    Jun 2, 2023 at 9:30
20

There's nothing wrong with the regex per se, it's your escaping that's at fault. You need to double escape the \ character since that's also a C++ string escape character.

Additionaly there is an edge case where this regex would think that 1. is a valid floating pointer number. So you might be better off with /^[0-9]+(\\.[0-9]+)?$ which eliminates that possibility.

5

Maybe not a direct answer, just useful information. The regexp:

std::regex rx(R"(^([+-]?(?:[[:d:]]+\.?|[[:d:]]*\.[[:d:]]+))(?:[Ee][+-]?[[:d:]]+)?$)");

matches strings:

"1", "0", "10",
"1000.1", "+1",
"+10", "-10", "1.",
".1", "1.1", "+1.",
"-1.", "+.1", "-.1",
"009", "+009", "-009",
"-01e0", "+01E0", "+1e-1",
"+1e+1", "+1.e1", "1E1",
"1E+1", "0.001e-12", "0.111111111111111"

and does not matches the next strings:

".", "1a", "++1",
"+-1", "+", "-.",
"-", "--1.", "1.e.1",
"1e.1", "0+.e0"

The first ones look like valid values for the double type in C++, e.g. double test = +009.e+10 is OK.

Play it in ideone.com: https://ideone.com/ooF8sG

4

/^[0-9]+.[0-9]+$ : use this for doubles.

accepts 123.123 types.

1
  • 4
    It should be \. otherwise you'd accept any decimal separator
    – 0x26res
    Aug 27, 2019 at 16:54
3
/^[0-9]*[.]?[0-9]+$/

Regex above works for doubles such as "45.5", "12", ".12"

0
(\d+)?\.(\d+)?

Regex above works for doubles such as "45.5", "12.", ".12"

1
  • 1
    it also accepts a single period character, which is definitely not a valid number.
    – ThatsMe
    May 17, 2021 at 8:46
0
    constexpr char double_regex_s[53] =
        "^" // accept only if it matches the beginning of the string
        "(-?)" // accept a single optional negation
        "(0|([1-9][0-9]*))" // number is either zero or some integer that does not start with zero
        "(" // begin optional decimals
            "\\." // require a dot
            "[0-9]+" // any digit is fine, but at least one (std::atof does not require but chrome requires digits after dot)
        ")?" // end optional decimals
        "(" // begin optional scientific exponent
            "[eE]" // require an e or E
            "[-+]?" // accept optional plus or minus
            "[0-9]+" // any digit is fine (tested in chrome JSON.parse(1E000003) works)
        ")?" // end optional scientific exponent
        "$" // accept only if it matches up to the end of the string
        ;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.