2

tIs it possible for me to detect if a string is 'all numeric' or not using tr1 regex? If yes, please help me with a snipped as well since I am new to regex.

Why I am looking towards tr1 regex for something like this, because I don't want to create a separate function for detecting if the string is numeric. I want to do it inline in rest of the client code but do not want it to look ugly as well. I feel maybe tr1 regex might help. Not sure, any advises on this?

bits
  • 7,480
  • 7
  • 43
  • 54
  • 3
    What does "numeric only" mean? If you mean "all of the characters are digits," why do you think a regular expression would be more effective than `std::find_if_not(begin(s), end(s), (int(*)(int))std::isdigit) == end(s)` – James McNellis Dec 12 '11 at 18:57
  • 1
    Is `1e6` numeric? Is `-1` numeric? – CanSpice Dec 12 '11 at 18:58
  • Yes I meant "all of the characters are digits". – bits Dec 12 '11 at 19:03
  • @JamesMcNellis I don't think regex would be better. I just didn't think it through to find an elegant enough solution. Your looks very elegant. Thanks. – bits Dec 12 '11 at 19:07
  • @bits when you say "*numeric only*" do you mean a string containing only digits (0-9) or should it be a valid (decimal) number? – Filip Roséen - refp Dec 12 '11 at 19:09
  • Please see: [What’s a Number?](http://stackoverflow.com/a/4247184/433790) – ridgerunner Dec 12 '11 at 22:46

3 Answers3

1

If you just want to test whether the string has all numeric characters, you can use std::find_if_not and std::isdigit:

std::find_if_not(s.begin(), s.end(), (int(*)(int))std::isdigit) == s.end()

If you do not have a Standard Library implementation with std::find_if_not, you can easily write it:

template <typename ForwardIt, typename Predicate>
ForwardIt find_if_not(ForwardIt first, ForwardIt last, Predicate pred)
{
    for (; first != last; ++first)
        if (!pred(first))
            return first;

    return first;
}
James McNellis
  • 327,682
  • 71
  • 882
  • 954
  • `std::find_if_not` is not mentioned in [c++ algorithm reference](http://www.cplusplus.com/reference/algorithm/). I am not sure why? I guess I will have to implement the template as you suggested. Thanks. – bits Dec 12 '11 at 19:25
  • @bits: It was added in C++11, and cplusplus.com is out of date. – James McNellis Dec 12 '11 at 19:27
1

You can use the string::find_first_not_of member function to test for numeric characters.

if (mystring.find_first_not_of("0123456789") == std::string::npos)
{
    std::cout << "numeric only!";
}
Blastfurnace
  • 17,441
  • 40
  • 50
  • 66
0

The regular expression for this is rather trivial. Just try to match "\\D". This will match on any character that's not a digit. If you'd like it to include a decimal separator too, you could use "[^\\d\\.]", which translates to "not a digit or dot".

However, how about simply using strtol() to read the number? You'll be able to retrieve a pointer to the first non-number character. So, if this points to the end of the string, it's been fine. Plus side here is, you won't even need TR1 for this.

Mario
  • 33,344
  • 5
  • 53
  • 74
  • Good point. But easy to do when reversing: The string is valid, if you get a match on `"^-?\\d+$"` or `"^-?\\d+(?:\\.\\d+)?$"` – Mario Dec 12 '11 at 19:18