gpt4 book ai didi

c++ - regex_match 抛出运行时异常

转载 作者:行者123 更新时间:2023-11-30 04:17:40 29 4
gpt4 key购买 nike

#include`<iostream>`
#include `<string>`
#include `<regex>`


using namespace std;



int main ()
{

try{



std::regex re("(http|https)://(\\w+\\.)*(\\w*)/([\\w\\d]+/{0,1})+");
if (std::regex_match ("http://www.google.com", re))
{
std::cout << "valid URL \n";
}

else
{
std::cout << "invalid URL \n";
}
}

catch(std::regex_error& e)
{

if (e.code() == std::regex_constants::error_brack)
std::cerr << "Problem with brackets--"<<e.code()<<"\n";

if (e.code() == std::regex_constants::error_collate)
std::cerr << "Problem error_collate--"<<e.code()<<"\n";

if (e.code() == std::regex_constants::error_ctype)
std::cerr << "Problem error_ctype--"<<e.code()<<"\n";

if (e.code() == std::regex_constants::error_escape)
std::cerr << "Problem error_escape--"<<e.code()<<"\n";

if (e.code() == std::regex_constants::error_backref)
std::cerr << "Problem error_backref--"<<e.code()<<"\n";


if (e.code() == std::regex_constants::error_paren)
std::cerr << "Problem error_paren--"<<e.code()<<"\n";

if (e.code() == std::regex_constants::error_brace)
std::cerr << "Problem error_brace--"<<e.code()<<"\n";

if (e.code() == std::regex_constants::error_badbrace)
std::cerr << "Problem error_badbrace--"<<e.code()<<"\n";

if (e.code() == std::regex_constants::error_range)
std::cerr << "Problem error_range--"<<e.code()<<"\n";

if (e.code() == std::regex_constants::error_space)
std::cerr << "Problem error_space--"<<e.code()<<"\n";




}

std::cout << std::endl;






return 0;
}

上面的代码有什么问题?

我用 g++ -std=gnu++0x testURL.cpp 编译了它

它编译得很好,但是当我尝试使用 ./a.out 执行时

它抛出与正则表达式转义序列相关的异常。

我应该更正 o/p 有效 url

正则表达式中的转义序列有问题吗?

我们如何解决?

最佳答案

你能试试这个正则表达式吗:

std::regex re("(http|https)://(\\w+\.)*(\\w*)/([\\w\\d]+/?)+");

关于c++ - regex_match 抛出运行时异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16958719/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com