Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我找到了应该匹配 IPv4 地址的正则表达式:
QRegExp rx_ipv4("^((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])$"); bool match = rx_ipv4.exactMatch("1.2.33333");
它返回真。
但是在上面的正则表达式中,以点结尾的部分必须重复三遍。这个正则表达式有什么问题?
上面的正则表达式在 C++ 代码中是不正确的。C++ 标准转义序列不包含:'\.'
C++ 转义序列
C++ 中正确的 IPv4 正则表达式是:
QRegExp rx_ipv4("^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\x2E){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$");
其中 \x2E 是 '.' 的 Ascii 代码。