/* * textregexreplace_noopt() * Return a string matched by a regular expression, with replacement. * * This version doesn't have an option argument: we default to case * sensitive match, replace the first instance only. */ datum_t textregexreplace_noopt(PG_FUNC_ARGS) { text *s = ARG_TEXT_PP(0); text *p = ARG_TEXT_PP(1); text *r = ARG_TEXT_PP(2); regex_t *re; re = RE_compile_and_cache(p, REG_ADVANCED, PG_COLLATION()); RET_TEXT_P(replace_text_regexp(s, (void *)re, r, false)); }
/* * textregexreplace_noopt() * Return a string matched by a regular expression, with replacement. * * This version doesn't have an option argument: we default to case * sensitive match, replace the first instance only. */ Datum textregexreplace_noopt(PG_FUNCTION_ARGS) { text *s = PG_GETARG_TEXT_P(0); text *p = PG_GETARG_TEXT_P(1); text *r = PG_GETARG_TEXT_P(2); regex_t *re; re = RE_compile_and_cache(p, regex_flavor); PG_RETURN_TEXT_P(replace_text_regexp(s, (void *) re, r, false)); }
/* * textregexreplace() * Return a string matched by a regular expression, with replacement. */ datum_t textregexreplace(PG_FUNC_ARGS) { text *s = ARG_TEXT_PP(0); text *p = ARG_TEXT_PP(1); text *r = ARG_TEXT_PP(2); text *opt = ARG_TEXT_PP(3); regex_t *re; pg_re_flags flags; parse_re_flags(&flags, opt); re = RE_compile_and_cache(p, flags.cflags, PG_COLLATION()); RET_TEXT_P(replace_text_regexp(s, (void *)re, r, flags.glob)); }
/* * textregexreplace() * Return a string matched by a regular expression, with replacement. */ Datum textregexreplace(PG_FUNCTION_ARGS) { text *s = PG_GETARG_TEXT_PP(0); text *p = PG_GETARG_TEXT_PP(1); text *r = PG_GETARG_TEXT_PP(2); text *opt = PG_GETARG_TEXT_PP(3); regex_t *re; pg_re_flags flags; parse_re_flags(&flags, opt); re = RE_compile_and_cache(p, flags.cflags); PG_RETURN_TEXT_P(replace_text_regexp(s, (void *) re, r, flags.glob)); }
/* * textregexreplace() * Return a string matched by a regular expression, with replacement. */ Datum textregexreplace(PG_FUNCTION_ARGS) { text *s = PG_GETARG_TEXT_P(0); text *p = PG_GETARG_TEXT_P(1); text *r = PG_GETARG_TEXT_P(2); text *opt = PG_GETARG_TEXT_P(3); char *opt_p = VARDATA(opt); int opt_len = (VARSIZE(opt) - VARHDRSZ); int i; bool glob = false; bool ignorecase = false; regex_t *re; /* parse options */ for (i = 0; i < opt_len; i++) { switch (opt_p[i]) { case 'i': ignorecase = true; break; case 'g': glob = true; break; default: ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("invalid option of regexp_replace: %c", opt_p[i]))); break; } } if (ignorecase) re = RE_compile_and_cache(p, regex_flavor | REG_ICASE); else re = RE_compile_and_cache(p, regex_flavor); PG_RETURN_TEXT_P(replace_text_regexp(s, (void *) re, r, glob)); }