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(®exp, "^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?", 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.
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(®exp, 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 :^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With