Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.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++ CPP&x2B;验证URL的正则表达式_C++_C_Regex_Mfc_Visual C++ - Fatal编程技术网

C++ CPP&x2B;验证URL的正则表达式

C++ CPP&x2B;验证URL的正则表达式,c++,c,regex,mfc,visual-c++,C++,C,Regex,Mfc,Visual C++,我想用c++{MFC}构建一个正则表达式来验证URL 正则表达式必须满足以下条件 有效URL:- http://www.google.com 无效的URL:- \MyWebSite/\ISAPIWEBSITE/\Denypage.aspx/=Regx必须检查“/\MyWebSite/\ISAPIWEBSITE/\Denypage.aspx/”之间的无效URL作为“\”字符(&F) =Regx必须检查URL并使其无效,因为URL中有多个“//”条目 =正则表达式必须检查URL并使其无效,以便额外

我想用c++{MFC}构建一个正则表达式来验证URL

正则表达式必须满足以下条件

有效URL:- http://www.google.com

无效的URL:-

  • \MyWebSite/\ISAPIWEBSITE/\Denypage.aspx/=Regx必须检查“/\MyWebSite/\ISAPIWEBSITE/\Denypage.aspx/”之间的无效URL作为“\”字符(&F)

  • =Regx必须检查URL并使其无效,因为URL中有多个“//”条目

  • =正则表达式必须检查URL并使其无效,以便额外插入%5C和%2F字符

  • 如何开发一个满足上述条件的泛型正则表达式。 请通过提供一个正则表达式来帮助我们,该正则表达式将在CPP{MFC}

    中处理上述场景。请看,右侧有一个社区选项卡,您可以在其中找到贡献的正则表达式。有一个URI类别,不确定您是否能准确找到所需内容,但这是一个良好的开端

    您是否尝试过使用该建议?如果您能够使用GCC-4.9,那么您可以直接使用

    它表示使用
    ^([^:/?#]+):(/([^/?#]*)?([^?#]*))([^?#]*)(\?([^?#]*))(#(.*))?
    可以作为子匹配获得:

      scheme    = $2
      authority = $4
      path      = $5
      query     = $7
      fragment  = $9
    
    例如:

    int main(int argc, char *argv[])
    {
      std::string url (argv[1]);
      unsigned counter = 0;
    
      std::regex url_regex (
        R"(^(([^:\/?#]+):)?(//([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?)",
        std::regex::extended
      );
      std::smatch url_match_result;
    
      std::cout << "Checking: " << url << std::endl;
    
      if (std::regex_match(url, url_match_result, url_regex)) {
        for (const auto& res : url_match_result) {
          std::cout << counter++ << ": " << res << std::endl;
        }
      } else {
        std::cerr << "Malformed url." << std::endl;
      }
    
      return EXIT_SUCCESS;
    }
    

    这真的很棒。您能告诉我如何使用正则表达式提取字符串中所有匹配的URL吗?我尝试将它与sregex_迭代器一起使用,但没有找到任何匹配项。非常感谢你!不幸的是,这不是为了验证,而是为了将一个正确的URI拆分为多个部分。它甚至检测不到最简单的情况,比如未编码的空间。谢谢你提供了这样一个有用且解释得很好的答案。这是我发现的最全面的URL解析脚本,它具有准确性、易用性和快速实现性。您不需要下载任何特殊的库!这将很好地回答这个问题:请注意,如果存在端口,它将包含在元素3和4中。即,
    http://localhost.com:8888/path?hue=br#cool
    导致
    3://localhost.com:8888
    4:localhost.com:8888
    。如果不起作用,此表达式将接受“aaa”
    ./url-matcher http://localhost.com/path\?hue\=br\#cool
    
    Checking: http://localhost.com/path?hue=br#cool
    0: http://localhost.com/path?hue=br#cool
    1: http:
    2: http
    3: //localhost.com
    4: localhost.com
    5: /path
    6: ?hue=br
    7: hue=br
    8: #cool
    9: cool