It depends on the regex implementation you're using. Here's an example using POSIX extended regular expressions that simply checks the return value of regcomp
and prints an error message obtained with regerror
:
#include <regex.h>
#include <stdio.h>
#include <string.h>
void test(const char *regex) {
regex_t preg;
int errcode = regcomp(&preg, regex, REG_EXTENDED);
if (errcode == 0) {
printf("%s => Success\n", regex);
regfree(&preg);
}
else {
char errmsg[80];
regerror(errcode, NULL, errmsg, 80);
printf("%s => %s\n", regex, errmsg);
}
}
int main() {
test("(*\\.com)");
test("(.*\\.com)");
return 0;
}
Try it online!
This should print something like:
(*\.com) => Invalid preceding regular expression
(.*\.com) => Success
Note that (*\.com)
is a valid POSIX basic regex since an unescaped (
matches a literal (
. With basic regexes, a *
at the beginning of a regex or parenthesized subexpression also matches a literal *
.