Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lHNi5-0004In-Fn for pgsql-hackers@arkaria.postgresql.org; Wed, 03 Mar 2021 09:22:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lHNi3-0007SB-0p for pgsql-hackers@arkaria.postgresql.org; Wed, 03 Mar 2021 09:22:23 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lHNi2-0007Ru-Iy for pgsql-hackers@lists.postgresql.org; Wed, 03 Mar 2021 09:22:22 +0000 Received: from mail-wr1-x432.google.com ([2a00:1450:4864:20::432]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lHNhz-0003RT-CB for pgsql-hackers@lists.postgresql.org; Wed, 03 Mar 2021 09:22:21 +0000 Received: by mail-wr1-x432.google.com with SMTP id u14so22842265wri.3 for ; Wed, 03 Mar 2021 01:22:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language; bh=obWRQsOEugBubWc4tdwc5amk+389M0eUbQ3VA06f0Bk=; b=CoxPclpqS5tsqj3FEe9OAiO+V8BXVAmkS358kI/Y5OeEkJvhZ6L26xb7emSaFBJ8g0 NcSDW4MhhKXHmclsZAmZjvl5Y07wuB0lj28Oze2Ir1duHQL62A1EtCm8ID+Rtls0w+AU +ciDbGl2kifdGdCY1KB0bpslXAuzpHOPDnLjkQGEfmhXRlkjeKEff8258RCkUfE2k4oT RS0WwAf4mIqwCFYkRw7f2vReIZKke99OPjDPq0+F4aTBuM7NXEiGvk2HFa13v6gnrLQ1 5DFYtFj+7Ju9qIQjBZ3BuVqb3FCT2tU66LG9zb++nWzn+yxyxHbaaWePZXEd2zajRywY OhhQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=obWRQsOEugBubWc4tdwc5amk+389M0eUbQ3VA06f0Bk=; b=LfOYVXcGW4LGOCOLmMK9okcLL+qv9t0q/Ej9vaFC2jKwll3+6psFshojvRotnvhLIL CtCIsrhPfkCl3gQklBJRCpqZoc1FY1gxTQR3+E8uQUVQaGIh7SEba/STPlksp50w8yVA kuW4b3q36em93smTRU4tmQ6i/sBdCEVVcd5cnBV4d833yZYvAZmiBFUws0BDZavIIhH9 xjsUEOLns3Vkvje1F32bMdCWNbOVZryz6YaS8rLhTF0U5eMsXQgvIE++PO3+rRL1Yml8 vs5779/I3TwO1aoaGCLp3ew6xmQ7LZIcJ0XllFlI9jRR3tZNQZh8SyE8jdPgBr7IjDQ6 Bgkw== X-Gm-Message-State: AOAM530iLd7w61uB+4t8k5pROUJUg5Ec9at9FqNQ+2uSXDAbSNsvB9R0 Odueyil4Pj3ZXXAYY1ZgpomGxa5d87IaOg== X-Google-Smtp-Source: ABdhPJwKnrh4oUmyAIwihf9+nlolybc3LawwtM1hpuWX+c0pM468kp+e1t/mIWZaMxVLBJx+9lu8OA== X-Received: by 2002:a5d:6443:: with SMTP id d3mr13457039wrw.292.1614763338057; Wed, 03 Mar 2021 01:22:18 -0800 (PST) Received: from ?IPv6:2a01:cb14:51d:5100:9d83:852a:e813:8d2b? ([2a01:cb14:51d:5100:9d83:852a:e813:8d2b]) by smtp.gmail.com with ESMTPSA id f7sm31290747wre.78.2021.03.03.01.22.17 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 03 Mar 2021 01:22:17 -0800 (PST) Subject: Re: [PATCH] proposal for regexp_count, regexp_instr, regexp_substr and regexp_replace To: pgsql-hackers@lists.postgresql.org References: From: Gilles Darold Message-ID: <79fadb31-3f06-3efc-bc53-1c4b015d5088@gmail.com> Date: Wed, 3 Mar 2021 10:22:16 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.7.1 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/mixed; boundary="------------36FCBCC49E5442E3B6D77715" Content-Language: fr List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------36FCBCC49E5442E3B6D77715 Content-Type: multipart/alternative; boundary="------------A1C798286C10CB8B42787022" --------------A1C798286C10CB8B42787022 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit My apologies for the links in the head, the email formatting and the missing patch, I accidently send the email too early. -- Gilles --------------A1C798286C10CB8B42787022 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 7bit

My apologies for the links in the head, the email formatting and the missing patch, I accidently send the email too early.

--

Gilles

--------------A1C798286C10CB8B42787022-- --------------36FCBCC49E5442E3B6D77715 Content-Type: text/x-patch; charset=UTF-8; name="regexp_functions-v1.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="regexp_functions-v1.patch" diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index bf99f82149..88e08b40d2 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -3097,6 +3097,47 @@ repeat('Pg', 4) PgPgPgPg + + + + regexp_count + + regexp_count ( string text, pattern text [, position integer ] [, flags text ] ) + integer + + + Return the number of times a pattern occurs for a match of a POSIX + regular expression to the string; see + . + + + regexp_count('123456789012', '\d{3}', 3) + 3 + + + + + + + regexp_instr + + regexp_instr ( string text, pattern text [, position integer ] [, occurence integer ] [, returnopt integer ] [, flags text ] [, group integer ] ) + integer + + + Return the position within string where the + match of a POSIX regular expression occurs. It returns an integer + indicating the beginning or ending position of the matched substring, + depending on the value of the returnopt argument + (default beginning). If no match is found, then the function returns 0; + see . + + + regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 0, 'i', 4) + 7 + + + @@ -3157,6 +3198,24 @@ repeat('Pg', 4) PgPgPgPg + + + + regexp_substr + + regexp_substr ( string text, pattern text [, position integer ] [, occurence integer ] [, flags text ] [, group integer ] ) + text + + + Return the substring within string corresponding to the + match of a POSIX regular expression; see . + + + regexp_substr('1234567890 1234557890', '(123)(4(5[56])(78))', 1, 2, 'i', 3) + 55 + + + @@ -5295,6 +5354,15 @@ substring('foobar' similar '#"o_b#"%' escape '#') NULL regexp_split_to_array + + regexp_count + + + regexp_instr + + + regexp_substr + lists the available @@ -5669,6 +5737,132 @@ SELECT foo FROM regexp_split_to_table('the quick brown fox', '\s*') AS foo; in practice. Other software systems such as Perl use similar definitions. + + The regexp_count function returns the number of + captured substring(s) resulting from matching a POSIX regular + expression pattern to a string. It has the syntax regexp_replace( + string, pattern , + position , flags ). + pattern is searched in string starting + from an optional position or from the beginning of string + by default. The flags parameter is an optional text string + containing zero or more single-letter flags that change the function's behavior. + regexp_count accepts all the flags + shown in . + The g flag is forced internally to count all matches. + This function returns 0 if there is no match or the number of match as + an integer. + + + + Some examples: + +SELECT regexp_count('123123123123', '\d{3}', 1); + regexp_count +-------------- + 4 +(1 row) + +SELECT regexp_count('Hello'||CHR(10)||'world!', '^world!$', 1, 'm'); + regexp_count +-------------- + 1 +(1 row) + + + + + The regexp_instr function returns the beginning or ending + position of the matched substring resulting from matching a POSIX regular + expression pattern to a string. It has the syntax regexp_instr( + string, pattern , + position , occurrence + , returnopt + , flags + , group ). + pattern is searched in string starting + from an optional position or from the beginning + of string by default. occurrence + indicates which occurrence of pattern in string + should be searched. When returnopt is set to 0 (default) the function + returns the position of the first character of the occurrence. When set to 1 returns the position + of the character after the occurrence. + The flags parameter is an optional text string + containing zero or more single-letter flags that change the function's behavior. + regexp_count accepts all the flags + shown in . + The g flag is forced internally to track all matches. + For a pattern with capture groups, group is an integer indicating + which capture in pattern is the target of the function. A capture group is a part of the pattern + enclosed in parentheses. Capture groups can be nested. They are numbered in order in which their + left parentheses appear in pattern. If group is zero, then the position + of the entire substring that matches the pattern is returned. If pattern + does not have at least group capture group, the function returns zero. + This function returns 0 if there is no match or the starting or ending position + of match as an integer. + + + + Some examples: + +SELECT regexp_instr('number of your street, zipcode thetown, FR', '[^ ]+', 1, 6); + regexp_instr +-------------- + 32 +(1 row) + +SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 0, 'i', 3); + regexp_instr +-------------- + 5 +(1 row) + + + + + The regexp_substr function returns the + matched substring resulting from matching a POSIX regular + expression pattern to a string. It has the syntax regexp_substr( + string, pattern , + position , occurrence + , flags + , group ). + pattern is searched in string starting + from an optional position or from the beginning + of string by default. occurrence + indicates which occurrence of pattern in string + should be searched. The flags parameter is an optional text string + containing zero or more single-letter flags that change the function's behavior. + regexp_count accepts all the flags + shown in . + The g flag is forced internally to track all matches. + For a pattern with capture groups, group is an integer indicating + which capture in pattern is the target of the function. A capture group is a part of the pattern + enclosed in parentheses. Capture groups can be nested. They are numbered in order in which their + left parentheses appear in pattern. If group is zero, then the position + of the entire substring that matches the pattern is returned. If pattern + does not have at least group capture group, the function returns zero. + This function returns NULL if there is no match or the substring of match. + + + + Some examples: + +SELECT regexp_substr('number of your street, zipcode town, FR', ',[^,]+', 1, 2); + regexp_substr +--------------- + , FR +(1 row) + +SELECT regexp_substr('1234567890 1234557890', '(123)(4(5[56])(78))', 1, 2, 'i', 3); + regexp_substr +--------------- + 55 +(1 row) + + + + diff --git a/src/backend/utils/adt/regexp.c b/src/backend/utils/adt/regexp.c index a32c5c82ab..42c25cdd75 100644 --- a/src/backend/utils/adt/regexp.c +++ b/src/backend/utils/adt/regexp.c @@ -120,6 +120,7 @@ static regexp_matches_ctx *setup_regexp_matches(text *orig_str, text *pattern, static ArrayType *build_regexp_match_result(regexp_matches_ctx *matchctx); static Datum build_regexp_split_result(regexp_matches_ctx *splitctx); +static text *enclose_with_parenthesis(text *str); /* * RE_compile_and_cache - compile a RE, caching if possible @@ -555,7 +556,6 @@ texticregexne(PG_FUNCTION_ARGS) 0, NULL)); } - /* * textregexsubstr() * Return a substring matched by a regular expression. @@ -1063,6 +1063,350 @@ regexp_matches_no_flags(PG_FUNCTION_ARGS) return regexp_matches(fcinfo); } +/* + * regexp_count() + * Return the number of matches of a pattern within a string. + */ +Datum +regexp_count(PG_FUNCTION_ARGS) +{ + text *orig_str = NULL; + text *pattern = PG_GETARG_TEXT_PP(1); + int start = 1; + text *flags = PG_GETARG_TEXT_PP_IF_EXISTS(3); + + pg_re_flags re_flags; + regexp_matches_ctx *matchctx; + + if (PG_NARGS() > 2) + start = PG_GETARG_INT32(2); + + if (start < 0) + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("negative start position not allowed."))); + + /* regexp_count(string, pattern, start[, flags]) */ + if (start > 1) + orig_str = DatumGetTextPP(DirectFunctionCall2(text_substr_no_len, + PG_GETARG_DATUM(0), + Int32GetDatum(start) + )); + else + orig_str = PG_GETARG_TEXT_PP(0); + + /* Determine options */ + parse_re_flags(&re_flags, flags); + + /* this function require flag 'g' */ + re_flags.glob = true; + + matchctx = setup_regexp_matches(orig_str, pattern, &re_flags, + PG_GET_COLLATION(), true, false, false); + + PG_RETURN_INT32(matchctx->nmatches); +} + +/* This is separate to keep the opr_sanity regression test from complaining */ +Datum +regexp_count_no_start(PG_FUNCTION_ARGS) +{ + return regexp_count(fcinfo); +} + + +/* This is separate to keep the opr_sanity regression test from complaining */ +Datum +regexp_count_no_flags(PG_FUNCTION_ARGS) +{ + return regexp_count(fcinfo); +} + +/* + * Return text string between parenthesis + */ +static text * +enclose_with_parenthesis(text *str) +{ + int len = VARSIZE_ANY_EXHDR(str); + text *result; + char *ptr; + + result = palloc(len + VARHDRSZ + 2); + SET_VARSIZE(result, len + VARHDRSZ + 2); + ptr = VARDATA(result); + memcpy(ptr, "(", 1); + memcpy(ptr+1, VARDATA_ANY(str), len); + memcpy(ptr+len+1, ")", 1); + + return result; +} + +/* + * regexp_instr() + * Return the position within the string where the match was located + */ +Datum +regexp_instr(PG_FUNCTION_ARGS) +{ + text *orig_str = NULL; + text *pattern = NULL; + text *flags = PG_GETARG_TEXT_PP_IF_EXISTS(5); + int start = 1; + int occurrence = 1; + int return_opt = 0; + int subexpr = 0; + int pos; + + pg_re_flags re_flags; + regexp_matches_ctx *matchctx; + + /* regexp_instr(string, pattern, start) */ + if (PG_NARGS() > 2) + start = PG_GETARG_INT32(2); + + if (start < 0) + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("negative start position not allowed."))); + + if (start > 1) + orig_str = DatumGetTextPP(DirectFunctionCall2(text_substr_no_len, + PG_GETARG_DATUM(0), + Int32GetDatum(start) + )); + else + /* regexp_instr(string, pattern) */ + orig_str = PG_GETARG_TEXT_PP(0); + + if (orig_str == NULL) + PG_RETURN_NULL(); + + /* regexp_instr(string, pattern, start, occurrence) */ + if (PG_NARGS() > 3) + occurrence = PG_GETARG_INT32(3); + + if (occurrence <= 0) + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("occurrence value must be greater than 0."))); + + /* regexp_instr(string, pattern, start, occurrence, return_opt) */ + if (PG_NARGS() > 4) + return_opt = PG_GETARG_INT32(4); + + if (return_opt != 0 && return_opt != 1) + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("return option value must be 0 or 1."))); + + /* regexp_instr(string, pattern, start, occurrence, return_opt, flags, subexpr) */ + if (PG_NARGS() > 6) + subexpr = PG_GETARG_INT32(6); + if (subexpr < 0) + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("number of capture group must be a positive value."))); + + /* + * If subexpr is zero (default), then the position of the entire + * substring that matches the pattern is returned. Otherwise we + * will exactly register the subexpressions given in the pattern. + * Enclose pattern between parenthesis to register the position + * of the entire substring. + */ + pattern = enclose_with_parenthesis(PG_GETARG_TEXT_PP(1)); + + /* Determine options */ + parse_re_flags(&re_flags, flags); + /* this function require flag 'g' */ + re_flags.glob = true; + + matchctx = setup_regexp_matches(orig_str, pattern, &re_flags, + PG_GET_COLLATION(), true, false, false); + + /* If no match is found, then the function returns 0 */ + if (matchctx->nmatches == 0) + PG_RETURN_INT32(0); + + /* When occurrence exceed matches return 0 */ + if (occurrence > matchctx->nmatches) + PG_RETURN_INT32(0); + + /* When subexpr exceed number of subexpression return 0 */ + if (subexpr > matchctx->npatterns - 1) + PG_RETURN_INT32(0); + + /* + * Returns the position of the first character of the occurrence + * or for subexpression in this occurrence. + */ + pos = (occurrence*matchctx->npatterns*2)-((matchctx->npatterns-subexpr)*2); + if (return_opt == 1) + pos += 1; + + PG_RETURN_INT32(matchctx->match_locs[pos]+start); +} + +/* This is separate to keep the opr_sanity regression test from complaining */ +Datum +regexp_instr_no_start(PG_FUNCTION_ARGS) +{ + return regexp_instr(fcinfo); +} + +/* This is separate to keep the opr_sanity regression test from complaining */ +Datum +regexp_instr_no_occurrence(PG_FUNCTION_ARGS) +{ + return regexp_instr(fcinfo); +} + +/* This is separate to keep the opr_sanity regression test from complaining */ +Datum +regexp_instr_no_return_opt(PG_FUNCTION_ARGS) +{ + return regexp_instr(fcinfo); +} + +/* This is separate to keep the opr_sanity regression test from complaining */ +Datum +regexp_instr_no_flags(PG_FUNCTION_ARGS) +{ + return regexp_instr(fcinfo); +} + +/* This is separate to keep the opr_sanity regression test from complaining */ +Datum +regexp_instr_no_subexpr(PG_FUNCTION_ARGS) +{ + return regexp_instr(fcinfo); +} + +/* + * regexp_substr() + * Return the substring within the string that match a regular + * expression pattern + */ +Datum +regexp_substr(PG_FUNCTION_ARGS) +{ + text *orig_str = NULL; + text *pattern = NULL; + text *flags = PG_GETARG_TEXT_PP_IF_EXISTS(4); + int start = 1; + int occurrence = 1; + int subexpr = 0; + int so, eo, pos; + + pg_re_flags re_flags; + regexp_matches_ctx *matchctx; + + if (PG_NARGS() > 2) + start = PG_GETARG_INT32(2); + + if (start < 0) + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("negative start position not allowed."))); + + /* regexp_substr(string, pattern, start) */ + if (start > 1) + orig_str = DatumGetTextPP(DirectFunctionCall2(text_substr_no_len, + PG_GETARG_DATUM(0), + Int32GetDatum(start) + )); + else + /* regexp_substr(string, pattern) */ + orig_str = PG_GETARG_TEXT_PP(0); + + if (orig_str == NULL) + PG_RETURN_NULL(); + + /* regexp_substr(string, pattern, start, occurrence) */ + if (PG_NARGS() > 3) + occurrence = PG_GETARG_INT32(3); + if (occurrence <= 0) + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("occurrence value must be greater than 0."))); + + /* regexp_substr(string, pattern, start, occurrence, flags, subexpr) */ + if (PG_NARGS() > 5) + subexpr = PG_GETARG_INT32(5); + if (subexpr < 0) + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("number of capture group must be a positive value."))); + + /* + * If subexpr is zero (default), then the position of the entire + * substring that matches the pattern is returned. Otherwise we + * will exactly register the subexpressions given in the pattern. + * Enclose pattern between parenthesis to register the position + * of the entire substring. + */ + pattern = enclose_with_parenthesis(PG_GETARG_TEXT_PP(1)); + + /* Determine options */ + parse_re_flags(&re_flags, flags); + /* this function require flag 'g' */ + re_flags.glob = true; + + matchctx = setup_regexp_matches(orig_str, pattern, &re_flags, + PG_GET_COLLATION(), true, false, false); + + /* If no match is found, then the function returns NULL */ + if (matchctx->nmatches == 0) + PG_RETURN_NULL(); + + /* When occurrence exceed matches return NULL */ + if (occurrence > matchctx->nmatches) + PG_RETURN_NULL(); + + /* When subexpr exceed number of subexpression return NULL */ + if (subexpr > matchctx->npatterns - 1) + PG_RETURN_NULL(); + + /* Returns the substring corresponding to the occurrence. */ + pos = (occurrence*matchctx->npatterns*2)-((matchctx->npatterns-subexpr)*2); + so = matchctx->match_locs[pos]+1; + eo = matchctx->match_locs[pos+1]+1; + + PG_RETURN_DATUM(DirectFunctionCall3(text_substr, + PointerGetDatum(matchctx->orig_str), + Int32GetDatum(so), + Int32GetDatum(eo - so))); +} + +/* This is separate to keep the opr_sanity regression test from complaining */ +Datum +regexp_substr_no_start(PG_FUNCTION_ARGS) +{ + return regexp_substr(fcinfo); +} + +/* This is separate to keep the opr_sanity regression test from complaining */ +Datum +regexp_substr_no_occurrence(PG_FUNCTION_ARGS) +{ + return regexp_substr(fcinfo); +} + +/* This is separate to keep the opr_sanity regression test from complaining */ +Datum +regexp_substr_no_flags(PG_FUNCTION_ARGS) +{ + return regexp_substr(fcinfo); +} + +/* This is separate to keep the opr_sanity regression test from complaining */ +Datum +regexp_substr_no_subexpr(PG_FUNCTION_ARGS) +{ + return regexp_substr(fcinfo); +} + /* * setup_regexp_matches --- do the initial matching for regexp_match * and regexp_split functions diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat index 3d3974f467..aae9226ec5 100644 --- a/src/include/catalog/pg_proc.dat +++ b/src/include/catalog/pg_proc.dat @@ -3557,6 +3557,48 @@ proname => 'regexp_matches', prorows => '10', proretset => 't', prorettype => '_text', proargtypes => 'text text text', prosrc => 'regexp_matches' }, +{ oid => '9614', descr => 'count match(es) for regexp', + proname => 'regexp_count', prorettype => 'int4', proargtypes => 'text text', + prosrc => 'regexp_count_no_start' }, +{ oid => '9615', descr => 'count match(es) for regexp', + proname => 'regexp_count', prorettype => 'int4', proargtypes => 'text text int4', + prosrc => 'regexp_count_no_flags' }, +{ oid => '9616', descr => 'count match(es) for regexp', + proname => 'regexp_count', prorettype => 'int4', + proargtypes => 'text text int4 text', prosrc => 'regexp_count' }, +{ oid => '9617', descr => 'position where the match for regexp was located', + proname => 'regexp_instr', prorettype => 'int4', proargtypes => 'text text', + prosrc => 'regexp_instr_no_start' }, +{ oid => '9618', descr => 'position where the match for regexp was located', + proname => 'regexp_instr', prorettype => 'int4', proargtypes => 'text text int4', + prosrc => 'regexp_instr_no_occurrence' }, +{ oid => '9619', descr => 'position where the match for regexp was located', + proname => 'regexp_instr', prorettype => 'int4', proargtypes => 'text text int4 int4', + prosrc => 'regexp_instr_no_return_opt' }, +{ oid => '9620', descr => 'position where the match for regexp was located', + proname => 'regexp_instr', prorettype => 'int4', proargtypes => 'text text int4 int4 int4', + prosrc => 'regexp_instr_no_flags' }, +{ oid => '9621', descr => 'position where the match for regexp was located', + proname => 'regexp_instr', prorettype => 'int4', proargtypes => 'text text int4 int4 int4 text', + prosrc => 'regexp_instr_no_subexpr' }, +{ oid => '9622', descr => 'position where the match for regexp was located', + proname => 'regexp_instr', prorettype => 'int4', proargtypes => 'text text int4 int4 int4 text int4', + prosrc => 'regexp_instr' }, +{ oid => '9623', descr => 'substring that match the regexp pattern', + proname => 'regexp_substr', prorettype => 'text', proargtypes => 'text text', + prosrc => 'regexp_substr_no_start' }, +{ oid => '9624', descr => 'substring that match the regexp pattern', + proname => 'regexp_substr', prorettype => 'text', proargtypes => 'text text int4', + prosrc => 'regexp_substr_no_occurrence' }, +{ oid => '9625', descr => 'substring that match the regexp pattern', + proname => 'regexp_substr', prorettype => 'text', proargtypes => 'text text int4 int4', + prosrc => 'regexp_substr_no_flags' }, +{ oid => '9626', descr => 'substring that match the regexp pattern', + proname => 'regexp_substr', prorettype => 'text', proargtypes => 'text text int4 int4 text', + prosrc => 'regexp_substr_no_subexpr' }, +{ oid => '9627', descr => 'substring that match the regexp pattern', + proname => 'regexp_substr', prorettype => 'text', proargtypes => 'text text int4 int4 text int4', + prosrc => 'regexp_substr' }, { oid => '2088', descr => 'split string by field_sep and return field_num', proname => 'split_part', prorettype => 'text', proargtypes => 'text text int4', prosrc => 'split_part' }, diff --git a/src/test/regress/expected/strings.out b/src/test/regress/expected/strings.out index fb4573d85f..99372be6f0 100644 --- a/src/test/regress/expected/strings.out +++ b/src/test/regress/expected/strings.out @@ -905,6 +905,393 @@ SELECT foo, length(foo) FROM regexp_split_to_table('thE QUick bROWn FOx jUMPs ov ERROR: regexp_split_to_table() does not support the "global" option SELECT regexp_split_to_array('thE QUick bROWn FOx jUMPs ovEr The lazy dOG', 'e', 'g'); ERROR: regexp_split_to_array() does not support the "global" option +-- count all matches for regexp +SELECT regexp_count('123123123123123', '(12)3'); + regexp_count +-------------- + 5 +(1 row) + +-- count all matches with start position +SELECT regexp_count('123123123123', '123', 0); + regexp_count +-------------- + 4 +(1 row) + +SELECT regexp_count('123123123123', '123', 1); + regexp_count +-------------- + 4 +(1 row) + +SELECT regexp_count('123123123123', '123', 3); + regexp_count +-------------- + 3 +(1 row) + +SELECT regexp_count('123123123123', '123', -3); +ERROR: negative start position not allowed. +-- count all matches in NULL string with a start position +SELECT regexp_count(NULL, '123', 3); + regexp_count +-------------- + \N +(1 row) + +-- count all matches with a start position greater than string length +SELECT regexp_count('123', '123', 10); + regexp_count +-------------- + 0 +(1 row) + +-- count all matches from different regexp +SELECT regexp_count('ABC123', '[A-Z]'), regexp_count('A1B2C3', '[A-Z]'); + regexp_count | regexp_count +--------------+-------------- + 3 | 3 +(1 row) + +SELECT regexp_count('ABC123', '[A-Z][0-9]'), regexp_count('A1B2C3', '[A-Z][0-9]'); + regexp_count | regexp_count +--------------+-------------- + 1 | 3 +(1 row) + +SELECT regexp_count('ABC123', '[A-Z][0-9]{2}'), regexp_count('A1B2C3', '[A-Z][0-9]{2}'); + regexp_count | regexp_count +--------------+-------------- + 1 | 0 +(1 row) + +SELECT regexp_count('ABC123', '([A-Z][0-9]){2}'), regexp_count('A1B2C3', '([A-Z][0-9]){2}'); + regexp_count | regexp_count +--------------+-------------- + 0 | 1 +(1 row) + +SELECT regexp_count('ABC123A5', '^[A-Z][0-9]'), regexp_count('A1B2C3', '^[A-Z][0-9]'); + regexp_count | regexp_count +--------------+-------------- + 0 | 1 +(1 row) + +SELECT regexp_count('ABC123', '[A-Z][0-9]{2}'), regexp_count('A1B2C34', '[A-Z][0-9]{2}'); + regexp_count | regexp_count +--------------+-------------- + 1 | 1 +(1 row) + +-- count matches with newline case insensivity +SELECT regexp_count('a'||CHR(10)||'d', 'a.d'); + regexp_count +-------------- + 1 +(1 row) + +SELECT regexp_count('a'||CHR(10)||'d', 'a.d', 1, 's'); + regexp_count +-------------- + 1 +(1 row) + +-- count matches with newline case sensivity +SELECT regexp_count('a'||CHR(10)||'d', 'a.d', 1, 'n'); + regexp_count +-------------- + 0 +(1 row) + +-- count not multiline matches +SELECT regexp_count('a'||CHR(10)||'d', '^d$'); + regexp_count +-------------- + 0 +(1 row) + +-- count multiline matches +SELECT regexp_count('a'||CHR(10)||'d', '^d$', 1, 'm'); + regexp_count +-------------- + 1 +(1 row) + +SELECT regexp_count('Hello'||CHR(10)||'world!', '^world!$', 1, 'm'); -- 1 + regexp_count +-------------- + 1 +(1 row) + +DROP TABLE IF EXISTS regexp_temp; +NOTICE: table "regexp_temp" does not exist, skipping +CREATE TABLE regexp_temp(fullname varchar(20), email varchar(20)); +INSERT INTO regexp_temp (fullname, email) VALUES ('John Doe', 'johndoe@example.com'); +INSERT INTO regexp_temp (fullname, email) VALUES ('Jane Doe', 'janedoe'); +-- count matches case sensitive +SELECT fullname, regexp_count(fullname, 'e', 1, 'c') FROM regexp_temp; + fullname | regexp_count +----------+-------------- + John Doe | 1 + Jane Doe | 2 +(2 rows) + +SELECT fullname, regexp_count(fullname, 'D', 1, 'c') FROM regexp_temp; + fullname | regexp_count +----------+-------------- + John Doe | 1 + Jane Doe | 1 +(2 rows) + +SELECT fullname, regexp_count(fullname, 'd', 1, 'c') FROM regexp_temp; + fullname | regexp_count +----------+-------------- + John Doe | 0 + Jane Doe | 0 +(2 rows) + +-- count matches case insensitive +SELECT fullname, regexp_count(fullname, 'E', 1, 'i') FROM regexp_temp; + fullname | regexp_count +----------+-------------- + John Doe | 1 + Jane Doe | 2 +(2 rows) + +SELECT fullname, regexp_count(fullname, 'do', 1, 'i') FROM regexp_temp; + fullname | regexp_count +----------+-------------- + John Doe | 1 + Jane Doe | 1 +(2 rows) + +-- return the start position of the 6th occurence starting at beginning of the string +SELECT regexp_instr('number of your street, zipcode thetown, FR', '[^ ]+', 1, 6); + regexp_instr +-------------- + 32 +(1 row) + +-- return the start position of the 5th occurence starting after the first word +SELECT regexp_instr('number of your street, zipcode thetown, FR', '[^ ]+', 7, 5, 0); + regexp_instr +-------------- + 32 +(1 row) + +-- return the ending position of the 5th occurence starting after the first word +SELECT regexp_instr('number of your street, zipcode thetown, FR', '[^ ]+', 7, 5, 0); + regexp_instr +-------------- + 32 +(1 row) + +-- return the ending position of the 2nd occurence starting after the first word +SELECT regexp_instr('number of your street, zipcode thetown, FR', '[T|Z|S][[:alpha:]]{5}', 7, 2, 1, 'i'); + regexp_instr +-------------- + 30 +(1 row) + +-- return the starting position corresponding to the different capture group +SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 0, 'i', 0); + regexp_instr +-------------- + 1 +(1 row) + +SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 0, 'i', 1); + regexp_instr +-------------- + 1 +(1 row) + +SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 0, 'i', 2); + regexp_instr +-------------- + 4 +(1 row) + +SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 0, 'i', 3); + regexp_instr +-------------- + 5 +(1 row) + +SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 0, 'i', 4); + regexp_instr +-------------- + 7 +(1 row) + +-- return the starting position corresponding to a non existant capture group +SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 0, 'i', 5); + regexp_instr +-------------- + 0 +(1 row) + +-- Same but with the ending position +SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 1, 'i', 0); + regexp_instr +-------------- + 9 +(1 row) + +SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 1, 'i', 1); + regexp_instr +-------------- + 4 +(1 row) + +SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 1, 'i', 2); + regexp_instr +-------------- + 9 +(1 row) + +SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 1, 'i', 3); + regexp_instr +-------------- + 7 +(1 row) + +SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 1, 'i', 4); + regexp_instr +-------------- + 9 +(1 row) + +SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 1, 'i', 5); + regexp_instr +-------------- + 0 +(1 row) + +-- start position of a valid email +SELECT email, regexp_instr(email, '\w+@\w+(\.\w+)+') "valid_email" FROM regexp_temp; + email | valid_email +---------------------+------------- + johndoe@example.com | 1 + janedoe | 0 +(2 rows) + +-- ending position of a valid email +SELECT email, regexp_instr(email, '\w+@\w+(\.\w+)+', 1, 1, 1) "valid_email" FROM regexp_temp; + email | valid_email +---------------------+------------- + johndoe@example.com | 20 + janedoe | 0 +(2 rows) + +-- start position of first capture group in the email (the dot part) +SELECT email, regexp_instr(email, '\w+@\w+(\.\w+)+', 1, 1, 0, 'i', 1) FROM regexp_temp; + email | regexp_instr +---------------------+-------------- + johndoe@example.com | 16 + janedoe | 0 +(2 rows) + +-- ending position of first capture group in the email (the dot part) +SELECT email, regexp_instr(email, '\w+@\w+(\.\w+)+', 1, 1, 1, 'i', 1) FROM regexp_temp; + email | regexp_instr +---------------------+-------------- + johndoe@example.com | 20 + janedoe | 0 +(2 rows) + +-- test negative values +SELECT regexp_instr('1234567890', '(123)(4(56)(78))', -1, 1, 1, 'i', 1); +ERROR: negative start position not allowed. +SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, -1, 1, 'i', 1); +ERROR: occurrence value must be greater than 0. +SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, -1, 'i', 1); +ERROR: return option value must be 0 or 1. +SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 1, 'i', -1); +ERROR: number of capture group must be a positive value. +-- return the substring matching the regexp +SELECT regexp_substr('number of your street, zipcode town, FR', ',[^,]+'); + regexp_substr +---------------- + , zipcode town +(1 row) + +-- return the substring matching the regexp +SELECT regexp_substr('number of your street, zipcode town, FR', ',[^,]+', 24); + regexp_substr +--------------- + , FR +(1 row) + +-- return the substring matching the regexp at the first occurrence +SELECT regexp_substr('number of your street, zipcode town, FR', ',[^,]+', 1, 1); + regexp_substr +---------------- + , zipcode town +(1 row) + +-- return the substring matching the regexp at the second occurrence +SELECT regexp_substr('number of your street, zipcode town, FR', ',[^,]+', 1, 2); + regexp_substr +--------------- + , FR +(1 row) + +-- case sensitivity substring search +SELECT regexp_substr('number of your street, zipcode town, FR', ',\s+[Zf][^,]+', 1, 1); + regexp_substr +--------------- + \N +(1 row) + +SELECT regexp_substr('number of your street, zipcode town, FR', ',\s+[Zf][^,]+', 1, 1, 'i'); + regexp_substr +---------------- + , zipcode town +(1 row) + +-- case sensitivity substring search with no capture group +SELECT regexp_substr('number of your street, zipcode town, FR', ',\s+[Zf][^,]+', 1, 1, 'i', 0); + regexp_substr +---------------- + , zipcode town +(1 row) + +-- case sensitivity substring search with non existing capture group +SELECT regexp_substr('number of your street, zipcode town, FR', ',\s+[Zf][^,]+', 1, 1, 'i', 0); + regexp_substr +---------------- + , zipcode town +(1 row) + +-- return the substring matching the regexp at different occurrence and capture group +SELECT regexp_substr('1234567890 1234567890', '(123)(4(56)(78))', 1, 1, 'i', 4); + regexp_substr +--------------- + 78 +(1 row) + +SELECT regexp_substr('1234567890 1234557890', '(123)(4(5[56])(78))', 1, 2, 'i', 3); + regexp_substr +--------------- + 55 +(1 row) + +SELECT regexp_substr('1234567890 1234567890', '(123)(4(56)(78))', 1, 1, 'i', 0); + regexp_substr +--------------- + 12345678 +(1 row) + +-- test negative values +SELECT regexp_substr('1234567890 1234567890', '(123)(4(56)(78))', -1, 1, 'i', 4); +ERROR: negative start position not allowed. +SELECT regexp_substr('1234567890 1234567890', '(123)(4(56)(78))', 1, -1, 'i', 4); +ERROR: occurrence value must be greater than 0. +SELECT regexp_substr('1234567890 1234567890', '(123)(4(56)(78))', 1, 1, 'i', -4); +ERROR: number of capture group must be a positive value. +DROP TABLE IF EXISTS regexp_temp; -- change NULL-display back \pset null '' -- E021-11 position expression diff --git a/src/test/regress/sql/strings.sql b/src/test/regress/sql/strings.sql index 57a48c9d0b..30d44cc6da 100644 --- a/src/test/regress/sql/strings.sql +++ b/src/test/regress/sql/strings.sql @@ -255,6 +255,110 @@ SELECT regexp_split_to_array('thE QUick bROWn FOx jUMPs ovEr The lazy dOG', 'e', SELECT foo, length(foo) FROM regexp_split_to_table('thE QUick bROWn FOx jUMPs ovEr The lazy dOG', 'e', 'g') AS foo; SELECT regexp_split_to_array('thE QUick bROWn FOx jUMPs ovEr The lazy dOG', 'e', 'g'); +-- count all matches for regexp +SELECT regexp_count('123123123123123', '(12)3'); +-- count all matches with start position +SELECT regexp_count('123123123123', '123', 0); +SELECT regexp_count('123123123123', '123', 1); +SELECT regexp_count('123123123123', '123', 3); +SELECT regexp_count('123123123123', '123', -3); +-- count all matches in NULL string with a start position +SELECT regexp_count(NULL, '123', 3); +-- count all matches with a start position greater than string length +SELECT regexp_count('123', '123', 10); +-- count all matches from different regexp +SELECT regexp_count('ABC123', '[A-Z]'), regexp_count('A1B2C3', '[A-Z]'); +SELECT regexp_count('ABC123', '[A-Z][0-9]'), regexp_count('A1B2C3', '[A-Z][0-9]'); +SELECT regexp_count('ABC123', '[A-Z][0-9]{2}'), regexp_count('A1B2C3', '[A-Z][0-9]{2}'); +SELECT regexp_count('ABC123', '([A-Z][0-9]){2}'), regexp_count('A1B2C3', '([A-Z][0-9]){2}'); +SELECT regexp_count('ABC123A5', '^[A-Z][0-9]'), regexp_count('A1B2C3', '^[A-Z][0-9]'); +SELECT regexp_count('ABC123', '[A-Z][0-9]{2}'), regexp_count('A1B2C34', '[A-Z][0-9]{2}'); +-- count matches with newline case insensivity +SELECT regexp_count('a'||CHR(10)||'d', 'a.d'); +SELECT regexp_count('a'||CHR(10)||'d', 'a.d', 1, 's'); +-- count matches with newline case sensivity +SELECT regexp_count('a'||CHR(10)||'d', 'a.d', 1, 'n'); +-- count not multiline matches +SELECT regexp_count('a'||CHR(10)||'d', '^d$'); +-- count multiline matches +SELECT regexp_count('a'||CHR(10)||'d', '^d$', 1, 'm'); +SELECT regexp_count('Hello'||CHR(10)||'world!', '^world!$', 1, 'm'); -- 1 + +DROP TABLE IF EXISTS regexp_temp; +CREATE TABLE regexp_temp(fullname varchar(20), email varchar(20)); +INSERT INTO regexp_temp (fullname, email) VALUES ('John Doe', 'johndoe@example.com'); +INSERT INTO regexp_temp (fullname, email) VALUES ('Jane Doe', 'janedoe'); +-- count matches case sensitive +SELECT fullname, regexp_count(fullname, 'e', 1, 'c') FROM regexp_temp; +SELECT fullname, regexp_count(fullname, 'D', 1, 'c') FROM regexp_temp; +SELECT fullname, regexp_count(fullname, 'd', 1, 'c') FROM regexp_temp; +-- count matches case insensitive +SELECT fullname, regexp_count(fullname, 'E', 1, 'i') FROM regexp_temp; +SELECT fullname, regexp_count(fullname, 'do', 1, 'i') FROM regexp_temp; + +-- return the start position of the 6th occurence starting at beginning of the string +SELECT regexp_instr('number of your street, zipcode thetown, FR', '[^ ]+', 1, 6); +-- return the start position of the 5th occurence starting after the first word +SELECT regexp_instr('number of your street, zipcode thetown, FR', '[^ ]+', 7, 5, 0); +-- return the ending position of the 5th occurence starting after the first word +SELECT regexp_instr('number of your street, zipcode thetown, FR', '[^ ]+', 7, 5, 0); +-- return the ending position of the 2nd occurence starting after the first word +SELECT regexp_instr('number of your street, zipcode thetown, FR', '[T|Z|S][[:alpha:]]{5}', 7, 2, 1, 'i'); +-- return the starting position corresponding to the different capture group +SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 0, 'i', 0); +SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 0, 'i', 1); +SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 0, 'i', 2); +SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 0, 'i', 3); +SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 0, 'i', 4); +-- return the starting position corresponding to a non existant capture group +SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 0, 'i', 5); +-- Same but with the ending position +SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 1, 'i', 0); +SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 1, 'i', 1); +SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 1, 'i', 2); +SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 1, 'i', 3); +SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 1, 'i', 4); +SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 1, 'i', 5); +-- start position of a valid email +SELECT email, regexp_instr(email, '\w+@\w+(\.\w+)+') "valid_email" FROM regexp_temp; +-- ending position of a valid email +SELECT email, regexp_instr(email, '\w+@\w+(\.\w+)+', 1, 1, 1) "valid_email" FROM regexp_temp; +-- start position of first capture group in the email (the dot part) +SELECT email, regexp_instr(email, '\w+@\w+(\.\w+)+', 1, 1, 0, 'i', 1) FROM regexp_temp; +-- ending position of first capture group in the email (the dot part) +SELECT email, regexp_instr(email, '\w+@\w+(\.\w+)+', 1, 1, 1, 'i', 1) FROM regexp_temp; +-- test negative values +SELECT regexp_instr('1234567890', '(123)(4(56)(78))', -1, 1, 1, 'i', 1); +SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, -1, 1, 'i', 1); +SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, -1, 'i', 1); +SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 1, 'i', -1); + +-- return the substring matching the regexp +SELECT regexp_substr('number of your street, zipcode town, FR', ',[^,]+'); +-- return the substring matching the regexp +SELECT regexp_substr('number of your street, zipcode town, FR', ',[^,]+', 24); +-- return the substring matching the regexp at the first occurrence +SELECT regexp_substr('number of your street, zipcode town, FR', ',[^,]+', 1, 1); +-- return the substring matching the regexp at the second occurrence +SELECT regexp_substr('number of your street, zipcode town, FR', ',[^,]+', 1, 2); +-- case sensitivity substring search +SELECT regexp_substr('number of your street, zipcode town, FR', ',\s+[Zf][^,]+', 1, 1); +SELECT regexp_substr('number of your street, zipcode town, FR', ',\s+[Zf][^,]+', 1, 1, 'i'); +-- case sensitivity substring search with no capture group +SELECT regexp_substr('number of your street, zipcode town, FR', ',\s+[Zf][^,]+', 1, 1, 'i', 0); +-- case sensitivity substring search with non existing capture group +SELECT regexp_substr('number of your street, zipcode town, FR', ',\s+[Zf][^,]+', 1, 1, 'i', 0); +-- return the substring matching the regexp at different occurrence and capture group +SELECT regexp_substr('1234567890 1234567890', '(123)(4(56)(78))', 1, 1, 'i', 4); +SELECT regexp_substr('1234567890 1234557890', '(123)(4(5[56])(78))', 1, 2, 'i', 3); +SELECT regexp_substr('1234567890 1234567890', '(123)(4(56)(78))', 1, 1, 'i', 0); +-- test negative values +SELECT regexp_substr('1234567890 1234567890', '(123)(4(56)(78))', -1, 1, 'i', 4); +SELECT regexp_substr('1234567890 1234567890', '(123)(4(56)(78))', 1, -1, 'i', 4); +SELECT regexp_substr('1234567890 1234567890', '(123)(4(56)(78))', 1, 1, 'i', -4); + +DROP TABLE IF EXISTS regexp_temp; + -- change NULL-display back \pset null '' --------------36FCBCC49E5442E3B6D77715--