Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 正则表达式未显示IPv6匹配的正确结果_C++_Regex_C++11_Sample - Fatal编程技术网

C++ 正则表达式未显示IPv6匹配的正确结果

C++ 正则表达式未显示IPv6匹配的正确结果,c++,regex,c++11,sample,C++,Regex,C++11,Sample,为什么在此示例中,第二个本地化字符串未在控制台中正确显示 Found 3 words IPv6 2001:0db8:0000:0000:0000:ff00:0042:8329 2001:0db8:: --- wrong output is here ::1 找到3个单词 2001:0db8:0000:0000:0000:ff00:0042:8329 2001:0db8::---此处的输出错误 ::1 #包括 #包括 #包括 #包括 int main() { std::string s=“ip

为什么在此示例中,第二个本地化字符串未在控制台中正确显示

Found 3 words IPv6 2001:0db8:0000:0000:0000:ff00:0042:8329 2001:0db8:: --- wrong output is here ::1 找到3个单词 2001:0db8:0000:0000:0000:ff00:0042:8329 2001:0db8::---此处的输出错误 ::1 #包括 #包括 #包括 #包括 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”; (0-0-9a-A-fA-fA-F[[0-9a-A-fA-F[1,4}:){{7,7}[0-9a-A-fA-F[[1,4}{{1,4}\124;(0-9 A-9a-A-A-A-fA-F[0-9a-A-fA-F[1,1,4}124;(0-1,4})10 10 10 10.10 10 10 10 10 10 10 10 10 10 10.10 10 10 10 10 10 10 10 10.10.10 10 10 10 10 10 10 10 10 10)10.10 10 10 10 10 10 10 10 10 10 10 10.10 10 10 10 10 10 10 10 10 10 10 10 10 10 10)互联网互联网互联网[[[[[0-1.1.1.5 5}}{[[0-0-0-0-0-9 A-A-A-A-A-A-A-A-F(0-9 A-9 A-A-A-A-A-A-A-A-A-A-A-A-A-A-A-A-F[1,4}{1,4}}{1,3}40;[0-9 A-9 A-A-A-A-A-A-A-F[[0-0-9 A-A-A-A-A-A-A-A-A-F[1,1,4}}}[1,1,3}[1,3}[1,3}}1245}(((([0-1,1,1,3},,[1,3},[1,3},,,[1,3},,,[0-3},[[[[0-3[0-1,3[0-1,3[1,3}}}}}}},,,[0-1,3}}},,,{1,4}:(:[0-9 A-A-A-A-A-A-A-A-A-A-A-A-A-A-A-A-A-A-A-A-A-A-F{1,4}{{1,7}1245}::)0-9-0-0-0-0-0-0-0-0-0-0-1-0-0-1-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-1-0-0-0-0-0-0-0-1-0-0-0-0-0-0-0-0-1-0-0-0-0-0-0-0-0-0-0-0-1-0-0-0-0-0-0-0-0-0-0-0-0-0-0-[0-0-0-0-0-9 0-0 0-0-0-0-9)5{[0-0-0[0-0-0 0-0 0-1[0-0[0-0-0 0-0-0-0-0-0-0-0-0-0-0-0-0-0 0-0-0-0-0 0-9)5 \[[0 0-0-0-0-0-0-0 0-0-0 0 0 0 0 0 0 0-9 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5{{[0[0 0-1[0 0 0 0-0 0 0 0 0 0-0 0 0-0 0 0 0 0 0[0[0-0-0 0 0 0 0 0-0 0 0 0 0[0[0[0-0 0-0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0(1-9); 自动字\u ipv6\u开始= std::sregex_迭代器(s.begin()、s.end()、ex_ipv6); 自动单词\u ipv6\u end=std::sregex\u迭代器(); if(std::regex_search(s,ex_ipv6)) {
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';
    }
  }
}

你能把没有正确显示的字符串和生成的输出放进去吗?你的正则表达式有657个字符长。你希望有人能帮你破译它吗?错误不在正则表达式中,而只是在打印结果中。好的。这个正则表达式工作:([0-9A-Fa-f]{1,4}:([0-9A-Fa-f]{1,4}:([0-9A-Fa-f]{1,4}:([0-9A-Fa-f]){1,4}.)[0-9 A-9 A-A-A-A-A-A-A-A-f-f[1,1,4}{1,4 0 0 0 0 0 0 0 0 0 0 0 1,4 5 5 0 0 0 0 0 0 0,4 5 5 5 5}[1,1,1,4}::[0-9 A-0-9 A-A-A-A-Fa-f{{{1,1,1,4}0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0})5 5})5 5 5 5??????????\????;;;;(;;;;;(;;;;;((((:(:[0-0-0-0-0-9 A-9 A-9 A-9 A-9 A-A-A-A-A-A-A-A-f-A-A-f-f-f(25[0-5]| | | | | | | | | | | | | | | | | | 25[0-5]| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
有些人在遇到问题时会想“我知道,我会使用正则表达式。”现在他们有两个问题。
~~Jamie Zawinski
#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:8329
2001:0db8::ff00:0042:8329
::1