Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why this regular expression will not compile?

Tags:

c++

regex

I would like to use regular expression from here:

https://www.rfc-editor.org/rfc/rfc3986#appendix-B

I am trying to compile it like this:

#include <regex.h>
...
regex_t regexp;
if((regcomp(&regexp, "^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?", REG_EXTENDED)) != 0){
    return SOME_ERROR:
}

But I am stuck with return value of regcomp:

REG_BADRPT

According to man it means:

Invalid use of repetition operators such as using * as the first character.

Similar meaning at this man:

?, * or + is not preceded by valid regular expression

I wrote parser using my own regular expression, but I would like to test this one too, since its officially in rfc. I do no intend to use it for validation though.

like image 651
rluks Avatar asked Oct 22 '22 16:10

rluks


1 Answers

As Oli Charlesworth suggested, you need to escape backslash \\ for the question marks \?. See C++ escape sequences for more information.

test program

#include <regex.h>
#include <iostream>

void test_regcomp(char *rx){
 regex_t regexp;
 if((regcomp(&regexp, rx, REG_EXTENDED)) != 0){
    std::cout << "ERROR :" << rx <<"\n";
 }
 else{
   std::cout <<  "   OK :"<< rx <<"\n";
 }
}

int main()
{

  char *rx1 = "^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?" ;
  char *rx2 = "^(([^:/\?#]+):)\?(//([^/\?#]*))\?([^\?#]*)(\\\?([^#]*))\?(#(.*))\?" ;

   test_regcomp(rx1);
   test_regcomp(rx2);

   return 0;
}

output

ERROR :^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(?([^#]*))?(#(.*))?
   OK :^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?

The \?in your regex is the source of the REG_BADRPT error. It gets converted to ?. If you replace it by \\?, regcomp will be able to compile your regex.

"^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?"

   OK :^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?
like image 91
David L. Avatar answered Oct 24 '22 11:10

David L.