为什么第二个本地化字符串在此示例的控制台中未正确显示?找到3个单词IPv62001:0db8:0000:0000:0000:ff00:0042:83292001:0db8::--错误的输出在这里:: 1#include #include #include #include int main(){ std::string s =“ipv4模型127.0.0.1” “直播2001:0db8:0000:0000:0000:ff00:0042:8329” “另一个2001:0db8::ff00:0042:8329” “zip格式:: 1”; std::regex ex_ipv6(“(([[0-9a-fA-F] {1,4}:){7,7} [0-9a-fA-F] {1,4} |([0-9a -fA-F] {1,4}:){1,7}:|([[0-9a-fA-F] {1,4}:){1,6}:[0-9a-fA-F ] {1,4} |([0-9a-fA-F] {1,4}:){1,5}(:[0-9a-fA-F] {1,4}){1,2 } |([0-9a-fA-F] {1,4}:){1,4}(:[0-9a-fA-F] {1,4}){1,3} |([0 -9a-fA-F] {1,4}:){1,3}(:[0-9a-fA-F] {1,4}){1,4} |([0-9a-fA- F] {1,4}:){1,2}(:[0-9a-fA-F] {1,4}){1,5} | [0-9a-fA-F] {1,4 }:((:[0-9a-fA-F] {1,4}){1,6})|:((([[0-9a-fA-F] {1,4}){1,7 } |:)| fe80:(:[0-9a-fA-F] {0,4}){0,4}%[0-9a-zA-Z] {1,} |::( ffff(: 0 {1,4}){0,1}:){0,1}((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])|([[0-9a-fA-F] {1,4}:){1,4}:((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]))“); 自动words_ipv6_begin = std::sregex_iterator(s.begin(),s.end(),ex_ipv6); 自动words_ipv6_end = std::sregex_iterator(); 如果(std::regex_search(s,ex_ipv6)) { std::cout 为(; words_ipv6_begin!= words_ipv6_end; words_ipv6_begin ++) { std::cout } }} 最佳答案 您的前缀检查接受字符串的时间比它早得多。表达式组([0-9a-fA-F]{1,4}:){1,7}:将跳转之前的地址标记为有效,使用了字符串,并检查::ff00:0042:8329与任何其他模式都不匹配(如果匹配,则也将视为单独的地址)。关于您的正则表达式用法的几点说明。 {7,7}可以简单地写为{7}。 IPv6地址在表示为base16时应为[0-9a-f],因此您可以放弃[A-F]检查。#include <algorithm>#include <iostream>#include <regex>#include <string>// RFC5952 outlines canonical formatting for rendering IPv6 addresses as// text. Hex values in an address SHOULD be lowercase. Addresses can be// shortened ONCE using the symbol '::'. Whitespace is actually processed// as part of the pattern, so use \\s to match whitespace.int main() { std::string ipv6 = "(?:" // For the first 6 fields, match addresses with no jump (::)... " (?: (?:[0-9a-f]{1,4}:){6}" // ...or a jump. " | :: (?:[0-9a-f]{1,4}:){5}" " | (?: [0-9a-f]{1,4})? :: (?:[0-9a-f]{1,4}:){4}" " | (?: (?:[0-9a-f]{1,4}:){0,1} [0-9a-f]{1,4})? :: (?:[0-9a-f]{1,4}:){3}" " | (?: (?:[0-9a-f]{1,4}:){0,2} [0-9a-f]{1,4})? :: (?:[0-9a-f]{1,4}:){2}" " | (?: (?:[0-9a-f]{1,4}:){0,3} [0-9a-f]{1,4})? :: (?:[0-9a-f]{1,4}:) " " | (?: (?:[0-9a-f]{1,4}:){0,4} [0-9a-f]{1,4})? :: " " ) " // Match the base10/16 addresses with no jump (suffix of above). " (?: [0-9a-f]{1,4} : [0-9a-f]{1,4} " " | (?: (?: 25[0-5] | 2[0-4][0-9] | [01]?[0-9]?[0-9])\\.){3} " " (?: (?: 25[0-5] | 2[0-4][0-9] | [01]?[0-9]?[0-9])) " " ) " // Not any above. Check to see if jump is between last 2 fields of addr. " | (?: (?:[0-9a-f]{1,4}:){0,5} [0-9a-f]{1,4})? :: [0-9a-f]{1,4} " " | (?: (?:[0-9a-f]{1,4}:){0,6} [0-9a-f]{1,4})? :: " ")"; // End of ipv6 string pattern. // Convert readable pattern above into the applicable regex pattern. ipv6.erase(std::remove_if(ipv6.begin(), ipv6.end(), ::isspace), ipv6.cend()); std::regex ipv6_pattern(ipv6); const std::string test = "ipv4 model 127.0.0.1 " "live 2001:0db8:0000:0000:0000:ff00:0042:8329 " "another 2001:0db8::ff00:0042:8329 " "zip form ::1 "; auto result = std::sregex_iterator(test.cbegin(), test.cend(), ipv6_pattern); const auto results_end = std::sregex_iterator(); if (result != results_end) { std::cout << "Found " << std::distance(result, results_end) << " IPv6 address matches.\n"; while (result != results_end) { std::cout << (*result++).str() << '\n'; } }}安慰:Found 3 IPv6 address matches.2001:0db8:0000:0000:0000:ff00:0042:83292001:0db8::ff00:0042:8329::1关于c++ - 正则表达式无法显示正确的IPv6匹配结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25408211/
10-10 21:29