如何查找给定的字符串是否符合十六进制表示法,例如。 0x34FF 没有正则表达式?
在正则表达式中它将是 0x[0-9a-fA-F]+ ,但是如何在纯 C++ 中实现它?
In regex it would be 0x[0-9a-fA-F]+
, but how to achieve it in pure c++ ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用它来代替 strspn 来检查有效的基础。
http://www.cplusplus.com/reference/cstring/strspn/
<强>下面的 C++ 示例此方法检查来自的任何基数有效基数
二进制转base32
You could use this instead strspn to check for valid base.
http://www.cplusplus.com/reference/cstring/strspn/
example below in C++ this method checks for any radix valid base from
binary-to-base32
您可以使用 std::count_if 来检查计数是否等于大小。
You could use
std::count_if
to check if the count equals the size.您可以使用 std::string 的内置方法来检查字符串的第一部分是否为文字“0x”,以及字符串的其余部分是否仅包含允许的字符。这是问题中给出的正则表达式的等效项:
You can use the built-in methods of
std::string
to check that the first portion of the string is the literal "0x" and that the remainder of the string contains only the allowed characters. Here is the equivalent to the regular expression given in the question:使用 C++11 你可以轻松做到这一点:
With C++11 you can do it easily:
调用
strtoul
并检查是否有错误。Call
strtoul
and check for an error.尝试以下操作
Try the following