Datum nameicregexne(PG_FUNCTION_ARGS) { Name n = PG_GETARG_NAME(0); text *p = PG_GETARG_TEXT_PP(1); PG_RETURN_BOOL(!RE_compile_and_execute(p, NameStr(*n), strlen(NameStr(*n)), REG_ADVANCED | REG_ICASE, PG_GET_COLLATION(), 0, NULL)); }
Datum textregexne(PG_FUNCTION_ARGS) { text *s = PG_GETARG_TEXT_PP(0); text *p = PG_GETARG_TEXT_PP(1); PG_RETURN_BOOL(!RE_compile_and_execute(p, VARDATA_ANY(s), VARSIZE_ANY_EXHDR(s), REG_ADVANCED, PG_GET_COLLATION(), 0, NULL)); }
/* * textregexsubstr() * Return a substring matched by a regular expression. */ Datum textregexsubstr(PG_FUNCTION_ARGS) { text *s = PG_GETARG_TEXT_P(0); text *p = PG_GETARG_TEXT_P(1); bool match; regmatch_t pmatch[2]; /* * We pass two regmatch_t structs to get info about the overall match and * the match for the first parenthesized subexpression (if any). If there * is a parenthesized subexpression, we return what it matched; else * return what the whole regexp matched. */ match = RE_compile_and_execute(p, VARDATA(s), VARSIZE(s) - VARHDRSZ, regex_flavor, 2, pmatch); /* match? then return the substring matching the pattern */ if (match) { int so, eo; so = pmatch[1].rm_so; eo = pmatch[1].rm_eo; if (so < 0 || eo < 0) { /* no parenthesized subexpression */ so = pmatch[0].rm_so; eo = pmatch[0].rm_eo; } return DirectFunctionCall3(text_substr, PointerGetDatum(s), Int32GetDatum(so + 1), Int32GetDatum(eo - so)); } PG_RETURN_NULL(); }
/* fixedlen_regexeq: a generic fixed length regexp routine s - the string to match against (not necessarily null-terminated) p - the pattern charlen - the length of the string */ static bool fixedlen_regexeq(char *s, struct varlena* p, int charlen, int cflags) { char *sterm; int result; if (!s || !p) return FALSE; /* be sure sterm is null-terminated */ sterm = (char *) palloc(charlen + 1); memset(sterm, 0, charlen + 1); strncpy(sterm, s, charlen); result = RE_compile_and_execute(p, sterm, cflags); pfree(sterm); return ((bool) result); }