Sarfraj
Sarfraj

Reputation: 312

regex_match throwing runtime exeception

#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;
}

What is wrong with the above code ?

I compiled it with g++ -std=gnu++0x testURL.cpp

It compiled well, but when I try to execute with ./a.out

It throws exception related to regex escape sequences.

What I should correct to o/p valid url

There is some problem with escape sequences in regex?

How we can resolve?

Upvotes: 1

Views: 221

Answers (2)

Cubbi
Cubbi

Reputation: 47428

First of all, forget about using std::regex with gcc; while some regex functions compile, they do not work.

Running your program with clang/libc++ or when using gcc but changing it from std::regex to boost::regex, I get "Invalid URL" output, and the reason for that is that neither the forward slash in your regex nor the part after it (/([\\w\\d]+/{0,1})+) appear in the string you're matching.

Upvotes: 1

anubhava
anubhava

Reputation: 785098

Can you try this regex:

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

Upvotes: 1

Related Questions