c++ - 使用 tr1 正则表达式查找字符串是否为 'numeric only'

标签 c++ regex string numeric tr1

我是否可以使用 tr1 正则表达式检测字符串是否为“全数字”? 如果是,请帮我剪一段,因为我是正则表达式的新手。

为什么我正在寻找类似这样的 tr1 正则表达式,因为我不想创建一个单独的函数来检测字符串是否为数字。我想在其余的客户端代码中内联,但又不想让它看起来很丑。我觉得 tr1 正则表达式可能会有所帮助。不确定,有什么建议吗?

最佳答案

如果只是想测试字符串是否全是数字字符,可以使用std::find_if_notstd::isdigit:

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

如果您没有使用 std::find_if_not 的标准库实现,您可以轻松地编写它:

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

关于c++ - 使用 tr1 正则表达式查找字符串是否为 'numeric only',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8479343/

相关文章:

c++ - 具有多个枚举的C++运行时类型切换

ruby - 转义的 Ruby 正则表达式在用作文字正则表达式时不匹配。为什么?

C++将字符串精确地转换为 double

c++ - Windows时区写入注册表是否可靠?

c++ - 使用opencv GPU旋转图像

regex - VB.Net正则表达式匹配

string - 在Scala中选择两个字符之间的子字符串

javascript - 为什么 JavaScript 的 string.split() 在某些情况下不能正常工作?

c++ - 关闭带有工作线程的控制台应用程序

javascript - 扩展正则表达式以包含时间格式 sss,th