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?
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;
}