#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
正则表达式中的转义序列有问题吗?
我们该如何解决?