Skip to content

Instantly share code, notes, and snippets.

@malcommac
Created October 24, 2019 11:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save malcommac/49ec95570915115fec39913a16f54228 to your computer and use it in GitHub Desktop.
Save malcommac/49ec95570915115fec39913a16f54228 to your computer and use it in GitHub Desktop.
sqlite3_create_function(dbSqlitez, "regexp", 2, SQLITE_ANY, NULL, &sqlite_regexp, NULL, NULL);
...
...
static void sqlite_regexp(sqlite3_context* context, int argc, sqlite3_value** values) {
if ( argc < 2 ) {
sqlite3_result_error(context, "SQL function regexp() called with missing arguments.", -1);
return;
}
char* reg = (char*)sqlite3_value_text(values[0]);
char* text = (char*)sqlite3_value_text(values[1]);
if ( argc != 2 || reg == 0 || text == 0) {
sqlite3_result_error(context, "SQL function regexp() called with invalid arguments.", -1);
return;
}
int ret;
regex_t regex;
ret = regcomp(&regex, reg, REG_EXTENDED | REG_NOSUB);
if ( ret != 0 ) {
sqlite3_result_error(context, "error compiling regular expression", -1);
return;
}
ret = regexec(&regex, text , 0, NULL, 0);
regfree(&regex);
sqlite3_result_int(context, (ret != REG_NOMATCH));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment