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 1mAH32-0000Si-2O for pgsql-hackers@arkaria.postgresql.org; Sun, 01 Aug 2021 19:22:56 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mAH2z-0007Ex-0I for pgsql-hackers@arkaria.postgresql.org; Sun, 01 Aug 2021 19:22:53 +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 1mAH2y-0007Ep-Kq for pgsql-hackers@lists.postgresql.org; Sun, 01 Aug 2021 19:22:52 +0000 Received: from 6.mo6.mail-out.ovh.net ([87.98.177.69]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mAH2v-0007nc-Gh for pgsql-hackers@lists.postgresql.org; Sun, 01 Aug 2021 19:22:52 +0000 Received: from player734.ha.ovh.net (unknown [10.110.115.215]) by mo6.mail-out.ovh.net (Postfix) with ESMTP id 48BE525B4B2 for ; Sun, 1 Aug 2021 21:22:47 +0200 (CEST) Received: from darold.net (lfbn-lyo-1-1368-22.w86-207.abo.wanadoo.fr [86.207.177.22]) (Authenticated sender: gilles@darold.net) by player734.ha.ovh.net (Postfix) with ESMTPSA id 9A02020A35D4A; Sun, 1 Aug 2021 19:22:42 +0000 (UTC) Authentication-Results: garm.ovh; auth=pass (GARM-103G00591a416b0-0b74-4285-8376-3a4ea426c17b, 433ABFECDF1D0E2370DDF409F74C69D95F7DE9A1) smtp.auth=gilles@darold.net X-OVh-ClientIp: 86.207.177.22 To: Tom Lane Cc: Chapman Flack , er@xs4all.nl, pgsql-hackers@lists.postgresql.org References: <2292303d-766d-2172-2942-b076a3bb6505@darold.net> <2122577845.234888.1616324857283@webmailclassic.xs4all.nl> <9f91e4e7-19c8-d8e0-6955-fefaebd4c8fe@darold.net> <60575B5A.4080807@anastigmatix.net> <345476.1616338397@sss.pgh.pa.us> <45fe2893-1efc-1666-3ea5-b5fb1918c7fa@gmail.com> <212635.1627329361@sss.pgh.pa.us> <2204c4a4-8ee8-abdd-d5ca-d0b6893f96ab@darold.net> <1124806.1627681104@sss.pgh.pa.us> From: Gilles Darold Subject: Re: [PATCH] proposal for regexp_count, regexp_instr, regexp_substr and regexp_replace Message-ID: Date: Sun, 1 Aug 2021 21:22:41 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 In-Reply-To: <1124806.1627681104@sss.pgh.pa.us> Content-Type: multipart/mixed; boundary="------------7953F524A7176BA9DC9ED9B3" Content-Language: fr X-Ovh-Tracer-Id: 17619770593995103855 X-VR-SPAMSTATE: OK X-VR-SPAMSCORE: -100 X-VR-SPAMCAUSE: gggruggvucftvghtrhhoucdtuddrgedvtddriedtgdejlecutefuodetggdotefrodftvfcurfhrohhfihhlvgemucfqggfjpdevjffgvefmvefgnecuuegrihhlohhuthemucehtddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjughrpefvfhfhuffkffgfgggjtgesmhdtreertdefjeenucfhrhhomhepifhilhhlvghsucffrghrohhlugcuoehgihhllhgvshesuggrrhholhgurdhnvghtqeenucggtffrrghtthgvrhhnpeeihfffudefueduueefheehveeuleduieetudefgfdvgfffjeeuffduffelgfduueenucfkpheptddrtddrtddrtddpkeeirddvtdejrddujeejrddvvdenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhhouggvpehsmhhtphdqohhuthdphhgvlhhopehplhgrhigvrhejfeegrdhhrgdrohhvhhdrnhgvthdpihhnvghtpedtrddtrddtrddtpdhmrghilhhfrhhomhepghhilhhlvghssegurghrohhlugdrnhgvthdprhgtphhtthhopehpghhsqhhlqdhhrggtkhgvrhhssehlihhsthhsrdhpohhsthhgrhgvshhqlhdrohhrgh List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------7953F524A7176BA9DC9ED9B3 Content-Type: multipart/alternative; boundary="------------8D1419F8BE945E390F4EE92C" --------------8D1419F8BE945E390F4EE92C Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Le 30/07/2021 =C3=A0 23:38, Tom Lane a =C3=A9crit=C2=A0: > Gilles Darold writes: >> Le 26/07/2021 =C3=A0 21:56, Tom Lane a =C3=A9crit=C2=A0: >>> I'm inclined to just drop the regexp_replace additions. I don't thin= k >>> that the extra parameters Oracle provides here are especially useful.= >>> They're definitely not useful enough to justify creating compatibilit= y >>> hazards for. >> I would not say that being able to replace the Nth occurrence of a >> pattern matching is not useful but i agree that this is not a common >> case with replacement. Both Oracle [1] and IBM DB2 [2] propose this fo= rm >> and I have though that we can not have compatibility issues because of= >> the different data type at the 4th parameter. > Well, here's an example of the potential issues: > > [...] Thanks for pointing me this case, I did not think that the prepared statement could lead to this confusion. >> Anyway, maybe we can just >> rename the function even if I would prefer that regexp_replace() be >> extended. For example: >> =C2=A0=C2=A0=C2=A0 regexp_replace(source, pattern, replacement [, flag= s ]); >> =C2=A0=C2=A0=C2=A0 regexp_substitute(source, pattern, replacement [, p= osition ] [, >> occurrence ] [, flags ]); > Hmm. Of course the entire selling point of this patch seems to be > bug-compatibility with Oracle, so using different names is largely > defeating the point :-( > > Maybe we should just hold our noses and do it. The point that > you'd get a recognizable failure if the wrong function were chosen > reassures me a little bit. We've seen a lot of cases where this > sort of ambiguity results in the system just silently doing something > different from what you expected, and I was afraid that that could > happen here. I join a new version of the patch that include a check of the option parameter in the basic form of regexp_replace() and return an error in ambiguous cases. PREPARE rr AS SELECT regexp_replace('healthy, wealthy, and wise','(\w+)thy', '\1ish', $1); EXECUTE rr(1); ERROR:=C2=A0 ambiguous use of the option parameter in regex_replace()= , value: 1 HINT:=C2=A0 you might set the occurrence parameter to force the use o= f the extended form of regex_replace() This is done by checking if the option parameter value is an integer and throw the error in this case. I don't think of anything better. Best regards, --=20 Gilles Darold --------------8D1419F8BE945E390F4EE92C Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit
Le 30/07/2021 à 23:38, Tom Lane a écrit :
Gilles Darold <gilles@darold.net> writes:
Le 26/07/2021 à 21:56, Tom Lane a écrit :
I'm inclined to just drop the regexp_replace additions.  I don't think
that the extra parameters Oracle provides here are especially useful.
They're definitely not useful enough to justify creating compatibility
hazards for.

      
I would not say that being able to replace the Nth occurrence of a
pattern matching is not useful but i agree that this is not a common
case with replacement. Both Oracle [1] and IBM DB2 [2] propose this form
and I have though that we can not have compatibility issues because of
the different data type at the 4th parameter.
Well, here's an example of the potential issues:

[...]


Thanks for pointing me this case, I did not think that the prepared statement could lead to this confusion.


Anyway, maybe we can just
rename the function even if I would prefer that regexp_replace() be
extended. For example:
    regexp_replace(source, pattern, replacement [, flags ]);
    regexp_substitute(source, pattern, replacement [, position ] [,
occurrence ] [, flags ]);
Hmm.  Of course the entire selling point of this patch seems to be
bug-compatibility with Oracle, so using different names is largely
defeating the point :-(

Maybe we should just hold our noses and do it.  The point that
you'd get a recognizable failure if the wrong function were chosen
reassures me a little bit.  We've seen a lot of cases where this
sort of ambiguity results in the system just silently doing something
different from what you expected, and I was afraid that that could
happen here.


I join a new version of the patch that include a check of the option parameter in the basic form of regexp_replace() and return an error in ambiguous cases.


PREPARE rr AS SELECT regexp_replace('healthy, wealthy, and wise','(\w+)thy', '\1ish', $1);
EXECUTE rr(1);
ERROR:  ambiguous use of the option parameter in regex_replace(), value: 1
HINT:  you might set the occurrence parameter to force the use of the extended form of regex_replace()


This is done by checking if the option parameter value is an integer and throw the error in this case. I don't think of anything better.


Best regards,

-- 
Gilles Darold
--------------8D1419F8BE945E390F4EE92C-- --------------7953F524A7176BA9DC9ED9B3 Content-Type: text/x-patch; charset=UTF-8; name="v5-0001-regexp-foo-functions.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="v5-0001-regexp-foo-functions.patch" diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index a5b6adc4bb..02d1f72e1e 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -3108,6 +3108,66 @@ repeat('Pg', 4) PgPgPgPg =20 + + + + regexp_count + + regexp_count ( string text, pattern text [, = position integer ] [, flag= s text ] ) + integer + + + Returns the number of times a pattern occurs for a match of a PO= SIX + regular expression to the string; see + . + + + regexp_count('123456789012', '\d{3}', 3) + 3 + + + + + + + regexp_instr + + regexp_instr ( string text, pattern text [, = position integer ] [, occu= rence integer ] [, returnopt integer ] [, flags text ] [, group integer ] ) + integer + + + Returns the position within string where t= he + match of a POSIX regular expression occurs. It returns an integer= + indicating the beginning or ending position of the matched substr= ing, + depending on the value of the returnopt ar= gument + (default beginning). If no match is found the function returns 0;= + see . + + + regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 0,= 'i', 4) + 7 + + + + + + + regexp_like + + regexp_like ( string= text, pattern text [, <= parameter>flags text ] ) + boolean + + + Evaluates the existence of a match to a POSIX regular expression= + in string; see . + + + regexp_like('Hello'||chr(10)||'world', '^world$', 'm')<= /literal> + t + + + + @@ -3156,7 +3216,7 @@ repeat('Pg', 4) PgPgPgPg= regexp_replace - regexp_replace ( string text, pattern text, = replacement text [, flags<= /parameter> text ] ) + regexp_replace ( string text, pattern text, = replacement text [, positi= on integer ] [, occurence= integer ] [, flags text ] ) text @@ -3171,6 +3231,24 @@ repeat('Pg', 4) PgPgPgPg =20 + + + + regexp_substr + + regexp_substr ( string text, pattern text [,= position integer ] [, occ= urence integer ] [, flags= text ] [, group integer= ] ) + text + + + Return the substring within string corresp= onding to the + match of a POSIX regular expression; see . + + + regexp_substr('1234567890 1234557890', '(123)(4(5[56])(= 78))', 1, 2, 'i', 3) + 55 + + + @@ -5392,6 +5470,18 @@ substring('foobar' similar '#"o_b#"%' escape '#') = NULL regexp_split_to_array + + regexp_like + + + regexp_count + + + regexp_instr + + + regexp_substr + =20 lists the available @@ -5650,6 +5740,8 @@ SELECT col1, (SELECT regexp_matches(col2, '(bar)(be= que)')) FROM tab; It has the syntax regexp_replace(source, pattern, replacement + , position + , occurrence , flags ). The source string is returned unchanged = if there is no match to the pattern. If th= ere is a @@ -5663,12 +5755,19 @@ SELECT col1, (SELECT regexp_matches(col2, '(bar)(= beque)')) FROM tab; substring matching the entire pattern should be inserted. Write \\ if you need to put a literal backslash in the= replacement text. - The flags parameter is an optional text - string containing zero or more single-letter flags that change the + pattern is searched in stri= ng starting + from an optional position or from the be= ginning + of source by default. Optional occurrence + parameter indicates which occurrence of pattern in + source should be replaced. Default valu= e for occurrence + is 1, replace only the first occurrence. The flags parameter + is an optional text string containing zero or more single-letter fl= ags that change the function's behavior. Flag i specifies case-inse= nsitive matching, while flag g specifies replacement of = each matching - substring rather than only the first one. Supported flags (though - not g) are + substring rather than only the first one. When occurre= nce + is set flag g has no effect. If occ= urrence + is set to zero, all occurrences are replaced which is similar to fl= ag g. + Supported flags (though not g) are described in . =20 @@ -5681,6 +5780,10 @@ regexp_replace('foobarbaz', 'b..', 'X', 'g') fooXX regexp_replace('foobarbaz', 'b(..)', 'X\1Y', 'g') fooXarYXazY +regexp_replace('A PostgreSQL function', 'a|e|i|o|u', 'X', 1, 0, 'i') + X PXstgrXSQL fXnctXXn= +regexp_replace('A PostgreSQL function', 'a|e|i|o|u', 'X', 1, 3, 'i') + A PostgrXSQL function= =20 @@ -5766,6 +5869,163 @@ SELECT foo FROM regexp_split_to_table('the quick = brown fox', '\s*') AS foo; in practice. Other software systems such as Perl use similar defini= tions. =20 + + The regexp_like function evaluates the existen= ce of a match + to a POSIX regular expression in string; ret= urns a boolean + resulting from matching a POSIX regular expression pattern to a str= ing. It has + the syntax regexp_like(string, + pattern , flags ). + pattern is searched in stri= ng starting + from the beginning of string.=20 + The flags parameter is an optional text = string + containing zero or more single-letter flags that change the functio= n's behavior. + regexp_like accepts all the flags + shown in . + This function is similar to regexp operator ~ wh= en used without + flags and similar to operator ~= * when + used with flags 'i' o= nly. + + + + Some examples: + +SELECT 'found' FROM (values('Hello'||chr(10)||'world') as f(col) WHERE r= egexp_like(col, '^world$', 'm'); + ?column? +---------- + found +(1 row) + + + + + + The regexp_count function returns the number o= f + captured substring(s) resulting from matching a POSIX regular + expression pattern to a string. It has the syntax regexp= _count( + string, pattern , + position , flags ). + pattern is searched in stri= ng starting + from an optional position or from the be= ginning of string + by default. The flags parameter is an o= ptional text string + containing zero or more single-letter flags that change the functio= n's behavior. + regexp_count accepts all the flags + shown in . + The g flag is forced internally to count all mat= ches. + This function returns 0 if there is no match or the number of match= es as + an integer. + + + + Some examples: + +SELECT regexp_count('123123123123', '\d{3}', 1); + regexp_count=20 +-------------- + 4 +(1 row) + +SELECT regexp_count('Hello'||CHR(10)||'world!', '^world!$', 1, 'm'); + regexp_count=20 +-------------- + 1 +(1 row) + + + + + The regexp_instr function returns the beginnin= g or ending + position of the matched substring resulting from matching a POSIX r= egular + expression pattern to a string. It has the syntax regexp= _instr( + string, pattern , + position , occurrence + , returnopt + , flags + , group ). + pattern is searched in stri= ng starting + from an optional position or from the be= ginning + of string by default. Optional occurrence parameter + indicates which occurrence of pattern in= string + should be searched. When optional returnopt parameter is set to 0 (default) + the function returns the position of the first character of the occ= urrence. 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 functio= n's behavior. + regexp_instr accepts all the flags + shown in . + The g flag is forced internally to track all mat= ches. + For a pattern with capture groups, group= is an integer indicating + which capture in pattern is the target o= f the function. + A capture group is a part of the pattern enclosed in parentheses. C= apture groups can be nested. + They are numbered in the order in which their left parentheses appe= ar 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 gro= ups, the function returns zero. + This function returns 0 if there is no match or the starting or end= ing position + of a match as an integer. + + + + Some examples: + +SELECT regexp_instr('number of your street, zipcode thetown, FR', '[^ ]+= ', 1, 6); + regexp_instr=20 +-------------- + 32 +(1 row) + +SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 0, 'i', 3); + regexp_instr=20 +-------------- + 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 stri= ng starting + from an optional position or from the be= ginning + of string by default. Optional paramete= r 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 functio= n's behavior. + regexp_substr accepts all the flags + shown in . + The g flag is forced internally to track all mat= ches. + For a pattern with capture groups, optional group is an integer indicating + which capture in pattern is the target o= f the function. A capture group + is a part of the pattern enclosed in parentheses. Capture groups ca= n be nested. They are numbered in + the order in which their left parentheses appear in pa= ttern. + 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 gro= ups, the function returns zero. + This function returns NULL if there is no match or the substring of= the match. + + + + Some examples: + +SELECT regexp_substr('number of your street, zipcode town, FR', ',[^,]+'= , 1, 2); + regexp_substr=20 +--------------- + , FR +(1 row) + +SELECT regexp_substr('1234567890 1234557890', '(123)(4(5[56])(78))', 1, = 2, 'i', 3); + regexp_substr=20 +--------------- + 55 +(1 row) + + + + =20 diff --git a/src/backend/utils/adt/regexp.c b/src/backend/utils/adt/regex= p.c index a32c5c82ab..f34cce18ef 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); =20 +static text *enclose_with_parenthesis(text *str); =20 /* * RE_compile_and_cache - compile a RE, caching if possible @@ -555,7 +556,6 @@ texticregexne(PG_FUNCTION_ARGS) 0, NULL)); } =20 - /* * textregexsubstr() * Return a substring matched by a regular expression. @@ -646,6 +646,32 @@ textregexreplace(PG_FUNCTION_ARGS) regex_t *re; pg_re_flags flags; =20 + + /* + * When the function is called with four parameters in a prepared + * statement the basic regexp_replace() function can be called + * instead of the extended form. This is because the parameter type + * is unknown at prepare time and the basic form is chosen. Check + * that the fourth parameter is not an integer otherwise return an + * error that we call the extended form. + */ + if (PG_NARGS() =3D=3D 4) + { + char *badopt; + + (void) strtol(TextDatumGetCString(opt), &badopt, 10); + if (strcmp(badopt, "") =3D=3D 0) + { + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("ambiguous use of the option parameter" + " in regex_replace(), value: %s",=20 + TextDatumGetCString(opt)), + errhint("you might set the occurrence parameter to force" + " the use of the extended form of regex_replace()"))); + } + } + parse_re_flags(&flags, opt); =20 re =3D RE_compile_and_cache(p, flags.cflags, PG_GET_COLLATION()); @@ -1063,6 +1089,508 @@ regexp_matches_no_flags(PG_FUNCTION_ARGS) return regexp_matches(fcinfo); } =20 +/* + * regexp_like() + * Return the true if a pattern match within a string. + */ +Datum +regexp_like(PG_FUNCTION_ARGS) +{ + text *str =3D PG_GETARG_TEXT_PP(0); + text *pattern =3D PG_GETARG_TEXT_PP(1); + text *flags =3D PG_GETARG_TEXT_PP_IF_EXISTS(2); + + pg_re_flags re_flags; + regexp_matches_ctx *matchctx; + + /* Determine options */ + parse_re_flags(&re_flags, flags); + + matchctx =3D setup_regexp_matches(str, pattern, &re_flags, + PG_GET_COLLATION(), true, false, false); + + if (matchctx->nmatches > 0) + PG_RETURN_BOOL(true); + + PG_RETURN_BOOL(false); +} + +/* This is separate to keep the opr_sanity regression test from complain= ing */ +Datum +regexp_like_no_flags(PG_FUNCTION_ARGS) +{ + return regexp_like(fcinfo); +} + +/* + * regexp_count() + * Return the number of matches of a pattern within a string. + */ +Datum +regexp_count(PG_FUNCTION_ARGS) +{ + text *orig_str =3D NULL; + text *pattern =3D PG_GETARG_TEXT_PP(1); + int start =3D 1; + text *flags =3D PG_GETARG_TEXT_PP_IF_EXISTS(3); + + pg_re_flags re_flags; + regexp_matches_ctx *matchctx; + + if (PG_NARGS() > 2) + start =3D PG_GETARG_INT32(2); + + if (start < 0) + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("invalid value for parameter \"%s\": %d", "start_position", = start))); + + /* regexp_count(string, pattern, start[, flags]) */ + if (start > 1) + orig_str =3D DatumGetTextPP(DirectFunctionCall2(text_substr_no_len, + PG_GETARG_DATUM(0), + Int32GetDatum(start) + )); + else + orig_str =3D PG_GETARG_TEXT_PP(0); + + /* Determine options */ + parse_re_flags(&re_flags, flags); + + /* this function require flag 'g' */ + re_flags.glob =3D true; + + matchctx =3D 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 complain= ing */ +Datum +regexp_count_no_start(PG_FUNCTION_ARGS) +{ + return regexp_count(fcinfo); +} + + +/* This is separate to keep the opr_sanity regression test from complain= ing */ +Datum +regexp_count_no_flags(PG_FUNCTION_ARGS) +{ + return regexp_count(fcinfo); +} + +/* + * textregexreplace_extended() + * Return a string matched by a regular expression, with replacement. + * Extends textregexreplace by allowing a start position and the + * choice of the occurrence to replace (0 means all occurrences). + */ +Datum +textregexreplace_extended(PG_FUNCTION_ARGS) +{ + text *s =3D NULL; + text *p =3D PG_GETARG_TEXT_PP(1); + text *pattern =3D enclose_with_parenthesis(PG_GETARG_TEXT_PP(1));= + text *r =3D PG_GETARG_TEXT_PP(2); + int start =3D 1; + int occurrence =3D 1; + int pos =3D 0; + text *flags =3D PG_GETARG_TEXT_PP_IF_EXISTS(5); + StringInfoData str; + regex_t *re; + pg_re_flags re_flags; + regexp_matches_ctx *matchctx; + text *after =3D NULL; + + /* start position */ + if (PG_NARGS() > 3) + start =3D PG_GETARG_INT32(3); + if (start < 0) + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("invalid value for parameter \"%s\": %d", "start_position", = start))); + if (start > 1) + s =3D DatumGetTextPP(DirectFunctionCall2(text_substr_no_len, + PG_GETARG_DATUM(0), + Int32GetDatum(start) + )); + else + s =3D PG_GETARG_TEXT_PP(0); + + /* occurrence to replace */ + if (PG_NARGS() > 4) + occurrence =3D PG_GETARG_INT32(4); + + if (occurrence < 0) + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("invalid value for parameter \"%s\": %d", "occurrence", occu= rrence))); + + /* Determine options */ + parse_re_flags(&re_flags, flags); + re_flags.glob =3D true; + + /* lookup for pattern */ + matchctx =3D setup_regexp_matches(s, pattern, &re_flags, + PG_GET_COLLATION(), true, + false, re_flags.glob); + + /* If no match is found, then the function returns the original string = */ + if (matchctx->nmatches =3D=3D 0) + PG_RETURN_DATUM(PG_GETARG_DATUM(0)); + + /* Get the position of the occurence to replace */ + if (PG_NARGS() > 4 && occurrence > 0) + { + /* When occurrence exceed matches return the original string */ + if (occurrence > matchctx->nmatches) + PG_RETURN_DATUM(PG_GETARG_DATUM(0)); + + pos =3D (occurrence*matchctx->npatterns*2)-(matchctx->npatterns*2); + pos =3D matchctx->match_locs[pos]+start; + } + else + pos =3D start; + + /* Normal case without explicit VARIADIC marker */ + initStringInfo(&str); + + /* Get the string before the occurrence starting */ + if (pos > 1) + { + text *before =3D DatumGetTextPP(DirectFunctionCall3(text_substr, + PG_GETARG_DATUM(0), + Int32GetDatum(1), + Int32GetDatum(pos - 1))); + appendStringInfoString(&str, TextDatumGetCString(before)); + } + + /* all occurences must be replaced? */ + if (occurrence =3D=3D 0) + re_flags.glob =3D true; + else + re_flags.glob =3D false; + + /* Compile the regular expression */ + re =3D RE_compile_and_cache(p, re_flags.cflags, PG_GET_COLLATION()); + + /* Get the substring starting at the right occurrence position */ + after =3D DatumGetTextPP(DirectFunctionCall2(text_substr_no_len, + PG_GETARG_DATUM(0), + Int32GetDatum(pos) + )); + + appendStringInfoString(&str, TextDatumGetCString(replace_text_regexp( + after, + (void *) re, + r, + re_flags.glob))); + + PG_RETURN_TEXT_P(CStringGetTextDatum(str.data)); +} + +/* This is separate to keep the opr_sanity regression test from complain= ing */ +Datum +textregexreplace_extended_no_occurrence(PG_FUNCTION_ARGS) +{ + return textregexreplace_extended(fcinfo); +} + +/* This is separate to keep the opr_sanity regression test from complain= ing */ +Datum +textregexreplace_extended_no_flags(PG_FUNCTION_ARGS) +{ + return textregexreplace_extended(fcinfo); +} + +/* + * Return text string between parenthesis + */ +static text * +enclose_with_parenthesis(text *str) +{ + int len =3D VARSIZE_ANY_EXHDR(str); + text *result; + char *ptr; + + result =3D palloc(len + VARHDRSZ + 2); + SET_VARSIZE(result, len + VARHDRSZ + 2); + ptr =3D 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 =3D NULL; + text *pattern =3D NULL; + text *flags =3D PG_GETARG_TEXT_PP_IF_EXISTS(5); + int start =3D 1; + int occurrence =3D 1; + int return_opt =3D 0; + int subexpr =3D 0; + int pos; + + pg_re_flags re_flags; + regexp_matches_ctx *matchctx; + + /* regexp_instr(string, pattern, start) */ + if (PG_NARGS() > 2) + start =3D PG_GETARG_INT32(2); + + if (start < 0) + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("invalid value for parameter \"%s\": %d", "start_position", = start))); + + if (start > 1) + orig_str =3D DatumGetTextPP(DirectFunctionCall2(text_substr_no_len, + PG_GETARG_DATUM(0), + Int32GetDatum(start) + )); + else + /* regexp_instr(string, pattern) */ + orig_str =3D PG_GETARG_TEXT_PP(0); + + if (orig_str =3D=3D NULL) + PG_RETURN_NULL(); + + /* regexp_instr(string, pattern, start, occurrence) */ + if (PG_NARGS() > 3) + occurrence =3D PG_GETARG_INT32(3); + + if (occurrence <=3D 0) + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("invalid value for parameter \"%s\": %d", "occurrence", occu= rrence))); + + /* regexp_instr(string, pattern, start, occurrence, return_opt) */ + if (PG_NARGS() > 4) + return_opt =3D PG_GETARG_INT32(4); + + if (return_opt !=3D 0 && return_opt !=3D 1) + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("invalid value for parameter \"%s\": %d", "return_option", r= eturn_opt))); + + /* regexp_instr(string, pattern, start, occurrence, return_opt, flags, = subexpr) */ + if (PG_NARGS() > 6) + subexpr =3D PG_GETARG_INT32(6); + if (subexpr < 0) + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("invalid value for parameter \"%s\": %d", "group", subexpr))= ); + + /* + * 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 =3D enclose_with_parenthesis(PG_GETARG_TEXT_PP(1)); + + /* Determine options */ + parse_re_flags(&re_flags, flags); + /* this function require flag 'g' */ + re_flags.glob =3D true; + + matchctx =3D 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 =3D=3D 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 =3D (occurrence*matchctx->npatterns*2)-((matchctx->npatterns-subexp= r)*2); + if (return_opt =3D=3D 1) + pos +=3D 1; + + PG_RETURN_INT32(matchctx->match_locs[pos]+start); +} + +/* This is separate to keep the opr_sanity regression test from complain= ing */ +Datum +regexp_instr_no_start(PG_FUNCTION_ARGS) +{ + return regexp_instr(fcinfo); +} + +/* This is separate to keep the opr_sanity regression test from complain= ing */ +Datum +regexp_instr_no_occurrence(PG_FUNCTION_ARGS) +{ + return regexp_instr(fcinfo); +} + +/* This is separate to keep the opr_sanity regression test from complain= ing */ +Datum +regexp_instr_no_return_opt(PG_FUNCTION_ARGS) +{ + return regexp_instr(fcinfo); +} + +/* This is separate to keep the opr_sanity regression test from complain= ing */ +Datum +regexp_instr_no_flags(PG_FUNCTION_ARGS) +{ + return regexp_instr(fcinfo); +} + +/* This is separate to keep the opr_sanity regression test from complain= ing */ +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 =3D NULL; + text *pattern =3D NULL; + text *flags =3D PG_GETARG_TEXT_PP_IF_EXISTS(4); + int start =3D 1; + int occurrence =3D 1; + int subexpr =3D 0; + int so, eo, pos; + + pg_re_flags re_flags; + regexp_matches_ctx *matchctx; + + if (PG_NARGS() > 2) + start =3D PG_GETARG_INT32(2); + + if (start < 0) + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("invalid value for parameter \"%s\": %d", "start_position", = start))); + + /* regexp_substr(string, pattern, start) */ + if (start > 1) + orig_str =3D DatumGetTextPP(DirectFunctionCall2(text_substr_no_len, + PG_GETARG_DATUM(0), + Int32GetDatum(start) + )); + else + /* regexp_substr(string, pattern) */ + orig_str =3D PG_GETARG_TEXT_PP(0); + + if (orig_str =3D=3D NULL) + PG_RETURN_NULL(); + + /* regexp_substr(string, pattern, start, occurrence) */ + if (PG_NARGS() > 3) + occurrence =3D PG_GETARG_INT32(3); + + if (occurrence <=3D 0) + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("invalid value for parameter \"%s\": %d", "occurrence", occu= rrence))); + + /* regexp_substr(string, pattern, start, occurrence, flags, subexpr) */= + if (PG_NARGS() > 5) + subexpr =3D PG_GETARG_INT32(5); + if (subexpr < 0) + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("invalid value for parameter \"%s\": %d", "group", subexpr))= ); + + /* + * 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 =3D enclose_with_parenthesis(PG_GETARG_TEXT_PP(1)); + + /* Determine options */ + parse_re_flags(&re_flags, flags); + /* this function require flag 'g' */ + re_flags.glob =3D true; + + matchctx =3D 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 =3D=3D 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 =3D (occurrence*matchctx->npatterns*2)-((matchctx->npatterns-subexp= r)*2); + so =3D matchctx->match_locs[pos]+1; + eo =3D 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 complain= ing */ +Datum +regexp_substr_no_start(PG_FUNCTION_ARGS) +{ + return regexp_substr(fcinfo); +} + +/* This is separate to keep the opr_sanity regression test from complain= ing */ +Datum +regexp_substr_no_occurrence(PG_FUNCTION_ARGS) +{ + return regexp_substr(fcinfo); +} + +/* This is separate to keep the opr_sanity regression test from complain= ing */ +Datum +regexp_substr_no_flags(PG_FUNCTION_ARGS) +{ + return regexp_substr(fcinfo); +} + +/* This is separate to keep the opr_sanity regression test from complain= ing */ +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_pro= c.dat index 8cd0252082..f45c98c05f 100644 --- a/src/include/catalog/pg_proc.dat +++ b/src/include/catalog/pg_proc.dat @@ -3579,6 +3579,63 @@ proname =3D> 'regexp_matches', prorows =3D> '10', proretset =3D> 't', prorettype =3D> '_text', proargtypes =3D> 'text text text', prosrc =3D> 'regexp_matches' }, +{ oid =3D> '9614', descr =3D> 'count match(es) for regexp', + proname =3D> 'regexp_count', prorettype =3D> 'int4', proargtypes =3D> = 'text text', + prosrc =3D> 'regexp_count_no_start' }, +{ oid =3D> '9615', descr =3D> 'count match(es) for regexp', + proname =3D> 'regexp_count', prorettype =3D> 'int4', proargtypes =3D> = 'text text int4', + prosrc =3D> 'regexp_count_no_flags' }, +{ oid =3D> '9616', descr =3D> 'count match(es) for regexp', + proname =3D> 'regexp_count', prorettype =3D> 'int4', + proargtypes =3D> 'text text int4 text', prosrc =3D> 'regexp_count' }, +{ oid =3D> '9617', descr =3D> 'position where the match for regexp was l= ocated', + proname =3D> 'regexp_instr', prorettype =3D> 'int4', proargtypes =3D> = 'text text', + prosrc =3D> 'regexp_instr_no_start' }, +{ oid =3D> '9618', descr =3D> 'position where the match for regexp was l= ocated', + proname =3D> 'regexp_instr', prorettype =3D> 'int4', proargtypes =3D> = 'text text int4', + prosrc =3D> 'regexp_instr_no_occurrence' }, +{ oid =3D> '9619', descr =3D> 'position where the match for regexp was l= ocated', + proname =3D> 'regexp_instr', prorettype =3D> 'int4', proargtypes =3D> = 'text text int4 int4', + prosrc =3D> 'regexp_instr_no_return_opt' }, +{ oid =3D> '9620', descr =3D> 'position where the match for regexp was l= ocated', + proname =3D> 'regexp_instr', prorettype =3D> 'int4', proargtypes =3D> = 'text text int4 int4 int4', + prosrc =3D> 'regexp_instr_no_flags' }, +{ oid =3D> '9621', descr =3D> 'position where the match for regexp was l= ocated', + proname =3D> 'regexp_instr', prorettype =3D> 'int4', proargtypes =3D> = 'text text int4 int4 int4 text', + prosrc =3D> 'regexp_instr_no_subexpr' }, +{ oid =3D> '9622', descr =3D> 'position where the match for regexp was l= ocated', + proname =3D> 'regexp_instr', prorettype =3D> 'int4', proargtypes =3D> = 'text text int4 int4 int4 text int4', + prosrc =3D> 'regexp_instr' }, +{ oid =3D> '9623', descr =3D> 'substring that matches the regexp pattern= ', + proname =3D> 'regexp_substr', prorettype =3D> 'text', proargtypes =3D>= 'text text', + prosrc =3D> 'regexp_substr_no_start' }, +{ oid =3D> '9624', descr =3D> 'substring that matchies the regexp patter= n', + proname =3D> 'regexp_substr', prorettype =3D> 'text', proargtypes =3D>= 'text text int4', + prosrc =3D> 'regexp_substr_no_occurrence' }, +{ oid =3D> '9625', descr =3D> 'substring that matches the regexp pattern= ', + proname =3D> 'regexp_substr', prorettype =3D> 'text', proargtypes =3D>= 'text text int4 int4', + prosrc =3D> 'regexp_substr_no_flags' }, +{ oid =3D> '9626', descr =3D> 'substring that matches the regexp pattern= ', + proname =3D> 'regexp_substr', prorettype =3D> 'text', proargtypes =3D>= 'text text int4 int4 text', + prosrc =3D> 'regexp_substr_no_subexpr' }, +{ oid =3D> '9627', descr =3D> 'substring that matches the regexp pattern= ', + proname =3D> 'regexp_substr', prorettype =3D> 'text', proargtypes =3D>= 'text text int4 int4 text int4', + prosrc =3D> 'regexp_substr' }, +{ oid =3D> '9628', descr =3D> 'evaluate match(es) for regexp', + proname =3D> 'regexp_like', prorettype =3D> 'bool', proargtypes =3D> '= text text', + prosrc =3D> 'regexp_like_no_flags' }, +{ oid =3D> '9629', descr =3D> 'evaluate match(es) for regexp', + proname =3D> 'regexp_like', prorettype =3D> 'bool', + proargtypes =3D> 'text text text', prosrc =3D> 'regexp_like' }, +{ oid =3D> '9606', descr =3D> 'replace text using regexp', + proname =3D> 'regexp_replace', prorettype =3D> 'text', + proargtypes =3D> 'text text text int4 int4 text', prosrc =3D> 'textreg= exreplace_extended' }, +{ oid =3D> '9607', descr =3D> 'replace text using regexp', + proname =3D> 'regexp_replace', prorettype =3D> 'text', + proargtypes =3D> 'text text text int4 int4', prosrc =3D> 'textregexrep= lace_extended_no_flags' }, +{ oid =3D> '9608', descr =3D> 'replace text using regexp', + proname =3D> 'regexp_replace', prorettype =3D> 'text', + proargtypes =3D> 'text text text int4', prosrc =3D> 'textregexreplace_= extended_no_occurrence' }, { oid =3D> '2088', descr =3D> 'split string by field_sep and return fiel= d_num', proname =3D> 'split_part', prorettype =3D> 'text', proargtypes =3D> 'text text int4', prosrc =3D> 'split_part' }, diff --git a/src/test/regress/expected/strings.out b/src/test/regress/exp= ected/strings.out index 91aa819804..b4e3501556 100644 --- a/src/test/regress/expected/strings.out +++ b/src/test/regress/expected/strings.out @@ -905,6 +905,757 @@ 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 dO= G', 'e', 'g'); ERROR: regexp_split_to_array() does not support the "global" option +-- regexp_like tests +SELECT regexp_like('a'||CHR(10)||'d', 'a.d'); + regexp_like=20 +------------- + t +(1 row) + +SELECT regexp_like('a'||CHR(10)||'d', 'a.d', 'm'); + regexp_like=20 +------------- + f +(1 row) + +SELECT regexp_like('a'||CHR(10)||'d', 'a.d', 'n'); + regexp_like=20 +------------- + f +(1 row) + +SELECT regexp_like('Steven', '^Ste(v|ph)en$'); + regexp_like=20 +------------- + t +(1 row) + +SELECT regexp_like('foo' || chr(10) || 'bar' || chr(10) || 'bequq' || ch= r(10) || 'baz', '^bar'); + regexp_like=20 +------------- + f +(1 row) + +SELECT regexp_like('foo' || chr(10) || 'bar' || chr(10) || 'bequq' || ch= r(10) || 'baz', 'bar'); + regexp_like=20 +------------- + t +(1 row) + +SELECT regexp_like('foo' || chr(10) || 'bar' || chr(10) || 'bequq' || ch= r(10) || 'baz', '^bar', 'm'); + regexp_like=20 +------------- + t +(1 row) + +SELECT regexp_like('foo' || chr(10) || 'bar' || chr(10) || 'bequq' || ch= r(10) || 'baz', '^bar', 'n'); + regexp_like=20 +------------- + t +(1 row) + +SELECT regexp_like('GREEN', '([aeiou])\1'); + regexp_like=20 +------------- + f +(1 row) + +SELECT regexp_like('GREEN', '([aeiou])\1', 'i'); + regexp_like=20 +------------- + t +(1 row) + +SELECT regexp_like('ORANGE' || chr(10) || 'GREEN', '([aeiou])\1', 'i'); + regexp_like=20 +------------- + t +(1 row) + +SELECT regexp_like('ORANGE' || chr(10) || 'GREEN', '^..([aeiou])\1', 'i'= ); + regexp_like=20 +------------- + f +(1 row) + +SELECT regexp_like('ORANGE' || chr(10) || 'GREEN', '([aeiou])\1', 'in');= + regexp_like=20 +------------- + t +(1 row) + +SELECT regexp_like('ORANGE' || chr(10) || 'GREEN', '^..([aeiou])\1', 'in= '); + regexp_like=20 +------------- + t +(1 row) + +SELECT regexp_like('ORANGE' || chr(10) || 'GREEN', '^..([aeiou])\1', 'im= '); + regexp_like=20 +------------- + t +(1 row) + +SELECT REGEXP_LIKE('abc', 'a b c'); + regexp_like=20 +------------- + f +(1 row) + +SELECT REGEXP_LIKE('abc', 'a b c','x'); + regexp_like=20 +------------- + t +(1 row) + +-- count all matches for regexp +SELECT regexp_count('123123123123123', '(12)3'); + regexp_count=20 +-------------- + 5 +(1 row) + +-- count all matches with start position +SELECT regexp_count('123123123123', '123', 0); + regexp_count=20 +-------------- + 4 +(1 row) + +SELECT regexp_count('123123123123', '123', 1); + regexp_count=20 +-------------- + 4 +(1 row) + +SELECT regexp_count('123123123123', '123', 3); + regexp_count=20 +-------------- + 3 +(1 row) + +SELECT regexp_count('123123123123', '123', -3); +ERROR: invalid value for parameter "start_position": -3 +-- count all matches in NULL string with a start position +SELECT regexp_count(NULL, '123', 3); + regexp_count=20 +-------------- + \N +(1 row) + +-- count all matches with a start position greater than string length +SELECT regexp_count('123', '123', 10); + regexp_count=20 +-------------- + 0 +(1 row) + +-- count all matches from different regexp +SELECT regexp_count('ABC123', '[A-Z]'), regexp_count('A1B2C3', '[A-Z]');= + regexp_count | regexp_count=20 +--------------+-------------- + 3 | 3 +(1 row) + +SELECT regexp_count('ABC123', '[A-Z][0-9]'), regexp_count('A1B2C3', '[A-= Z][0-9]'); + regexp_count | regexp_count=20 +--------------+-------------- + 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=20 +--------------+-------------- + 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=20 +--------------+-------------- + 0 | 1 +(1 row) + +SELECT regexp_count('ABC123A5', '^[A-Z][0-9]'), regexp_count('A1B2C3', '= ^[A-Z][0-9]'); + regexp_count | regexp_count=20 +--------------+-------------- + 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=20 +--------------+-------------- + 1 | 1 +(1 row) + +-- count matches with newline case insensivity +SELECT regexp_count('a'||CHR(10)||'d', 'a.d'); + regexp_count=20 +-------------- + 1 +(1 row) + +SELECT regexp_count('a'||CHR(10)||'d', 'a.d', 1, 's'); + regexp_count=20 +-------------- + 1 +(1 row) + +-- count matches with newline case sensivity +SELECT regexp_count('a'||CHR(10)||'d', 'a.d', 1, 'n'); + regexp_count=20 +-------------- + 0 +(1 row) + +-- count not multiline matches +SELECT regexp_count('a'||CHR(10)||'d', '^d$'); + regexp_count=20 +-------------- + 0 +(1 row) + +-- count multiline matches +SELECT regexp_count('a'||CHR(10)||'d', '^d$', 1, 'm'); + regexp_count=20 +-------------- + 1 +(1 row) + +SELECT regexp_count('Hello'||CHR(10)||'world!', '^world!$', 1, 'm'); -- = 1 + regexp_count=20 +-------------- + 1 +(1 row) + +-- Count the number of occurrences of the substring an in the string. +SELECT regexp_count('a man, a plan, a canal: Panama', 'an'); + regexp_count=20 +-------------- + 4 +(1 row) + +-- Find the number of occurrences of the substring an in the string star= ting with the fifth character. +SELECT regexp_count('a man, a plan, a canal: Panama', 'an',5); + regexp_count=20 +-------------- + 3 +(1 row) + +-- Find the number of occurrences of a substring containing a lower-case= character +-- followed by an. In the first example, do not use a modifier. In the s= econd example, +-- use the i modifier to force the regular expression to ignore case. +SELECT regexp_count('a man, a plan, a canal: Panama', '[a-z]an'); + regexp_count=20 +-------------- + 3 +(1 row) + +SELECT regexp_count('a man, a plan, a canal: Panama', '[a-z]an', 1, 'i')= ; + regexp_count=20 +-------------- + 4 +(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@e= xample.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=20 +----------+-------------- + John Doe | 1 + Jane Doe | 2 +(2 rows) + +SELECT fullname, regexp_count(fullname, 'D', 1, 'c') FROM regexp_temp; + fullname | regexp_count=20 +----------+-------------- + John Doe | 1 + Jane Doe | 1 +(2 rows) + +SELECT fullname, regexp_count(fullname, 'd', 1, 'c') FROM regexp_temp; + fullname | regexp_count=20 +----------+-------------- + 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=20 +----------+-------------- + John Doe | 1 + Jane Doe | 2 +(2 rows) + +SELECT fullname, regexp_count(fullname, 'do', 1, 'i') FROM regexp_temp; + fullname | regexp_count=20 +----------+-------------- + 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=20 +-------------- + 32 +(1 row) + +-- return the start position of the 5th occurence starting after the fir= st word +SELECT regexp_instr('number of your street, zipcode thetown, FR', '[^ ]+= ', 7, 5, 0); + regexp_instr=20 +-------------- + 32 +(1 row) + +-- return the ending position of the 5th occurence starting after the fi= rst word +SELECT regexp_instr('number of your street, zipcode thetown, FR', '[^ ]+= ', 7, 5, 0); + regexp_instr=20 +-------------- + 32 +(1 row) + +-- return the ending position of the 2nd occurence starting after the fi= rst word +SELECT regexp_instr('number of your street, zipcode thetown, FR', '[T|Z|= S][[:alpha:]]{5}', 7, 2, 1, 'i'); + regexp_instr=20 +-------------- + 30 +(1 row) + +-- return the starting position corresponding to the different capture g= roup +SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 0, 'i', 0); + regexp_instr=20 +-------------- + 1 +(1 row) + +SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 0, 'i', 1); + regexp_instr=20 +-------------- + 1 +(1 row) + +SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 0, 'i', 2); + regexp_instr=20 +-------------- + 4 +(1 row) + +SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 0, 'i', 3); + regexp_instr=20 +-------------- + 5 +(1 row) + +SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 0, 'i', 4); + regexp_instr=20 +-------------- + 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=20 +-------------- + 0 +(1 row) + +-- Same but with the ending position +SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 1, 'i', 0); + regexp_instr=20 +-------------- + 9 +(1 row) + +SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 1, 'i', 1); + regexp_instr=20 +-------------- + 4 +(1 row) + +SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 1, 'i', 2); + regexp_instr=20 +-------------- + 9 +(1 row) + +SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 1, 'i', 3); + regexp_instr=20 +-------------- + 7 +(1 row) + +SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 1, 'i', 4); + regexp_instr=20 +-------------- + 9 +(1 row) + +SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 1, 'i', 5); + regexp_instr=20 +-------------- + 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=20 +---------------------+------------- + 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_ema= il" FROM regexp_temp; + email | valid_email=20 +---------------------+------------- + 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) FR= OM regexp_temp; + email | regexp_instr=20 +---------------------+-------------- + 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) FR= OM regexp_temp; + email | regexp_instr=20 +---------------------+-------------- + 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: invalid value for parameter "start_position": -1 +SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, -1, 1, 'i', 1);= +ERROR: invalid value for parameter "occurrence": -1 +SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, -1, 'i', 1);= +ERROR: invalid value for parameter "return_option": -1 +SELECT regexp_instr('1234567890', '(123)(4(56)(78))', 1, 1, 1, 'i', -1);= +ERROR: invalid value for parameter "group": -1 +-- Find the first occurrence of a sequence of letters starting with the = letter e +-- and ending with the letter y in the phrase "easy come, easy go." +SELECT regexp_instr('easy come, easy go','e\w*y'); + regexp_instr=20 +-------------- + 1 +(1 row) + +-- Find the first sequence of letters starting with the letter e and end= ing with +-- the letter y in the string "easy come, easy go" starting at the secon= d character +SELECT regexp_instr('easy come, easy go','e\w*y',2); + regexp_instr=20 +-------------- + 12 +(1 row) + +-- Find the second sequence of letters starting with the letter e and en= ding with +-- the letter y in the string "easy come, easy go" starting at the first= character. +SELECT regexp_instr('easy come, easy go','e\w*y',1,2); + regexp_instr=20 +-------------- + 12 +(1 row) + +-- Find the position of the first character after the first whitespace i= n the string "easy come, easy go." +SELECT regexp_instr('easy come, easy go','\s',1,1,1); + regexp_instr=20 +-------------- + 6 +(1 row) + +-- Find the position of the start of the third word in a string by captu= ring each +-- word as a subexpression, and returning the third subexpression's star= t position. +SELECT regexp_instr('one two three','(\w+)\s+(\w+)\s+(\w+)', 1,1,0,'',3)= ; + regexp_instr=20 +-------------- + 9 +(1 row) + +-- return the substring matching the regexp +SELECT regexp_substr('number of your street, zipcode town, FR', ',[^,]+'= ); + regexp_substr =20 +---------------- + , zipcode town +(1 row) + +-- return the substring matching the regexp +SELECT regexp_substr('number of your street, zipcode town, FR', ',[^,]+'= , 24); + regexp_substr=20 +--------------- + , 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 =20 +---------------- + , 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=20 +--------------- + , FR +(1 row) + +-- case sensitivity substring search +SELECT regexp_substr('number of your street, zipcode town, FR', ',\s+[Zf= ][^,]+', 1, 1); + regexp_substr=20 +--------------- + \N +(1 row) + +SELECT regexp_substr('number of your street, zipcode town, FR', ',\s+[Zf= ][^,]+', 1, 1, 'i'); + regexp_substr =20 +---------------- + , 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 =20 +---------------- + , 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 =20 +---------------- + , 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=20 +--------------- + 78 +(1 row) + +SELECT regexp_substr('1234567890 1234557890', '(123)(4(5[56])(78))', 1, = 2, 'i', 3); + regexp_substr=20 +--------------- + 55 +(1 row) + +SELECT regexp_substr('1234567890 1234567890', '(123)(4(56)(78))', 1, 1, = 'i', 0); + regexp_substr=20 +--------------- + 12345678 +(1 row) + +-- test negative values +SELECT regexp_substr('1234567890 1234567890', '(123)(4(56)(78))', -1, 1,= 'i', 4); +ERROR: invalid value for parameter "start_position": -1 +SELECT regexp_substr('1234567890 1234567890', '(123)(4(56)(78))', 1, -1,= 'i', 4); +ERROR: invalid value for parameter "occurrence": -1 +SELECT regexp_substr('1234567890 1234567890', '(123)(4(56)(78))', 1, 1, = 'i', -4); +ERROR: invalid value for parameter "group": -4 +-- Select the first substring of letters that end with "thy." +SELECT regexp_substr('healthy, wealthy, and wise','\w+thy'); + regexp_substr=20 +--------------- + healthy +(1 row) + +-- Select the first substring of letters that ends with "thy" starting a= t the second character in the string. +SELECT regexp_substr('healthy, wealthy, and wise','\w+thy',2); + regexp_substr=20 +--------------- + ealthy +(1 row) + +-- Return the contents of the third captured subexpression, which captur= es the third word in the string. +SELECT regexp_substr('one two three', '(\w+)\s+(\w+)\s+(\w+)', 1, 1, '',= 3); + regexp_substr=20 +--------------- + three +(1 row) + +DROP TABLE IF EXISTS regexp_temp; +-- Regression tests for extended regexp_replace() function with start po= sition and occurrence +SELECT regexp_replace('512.123.4567', '([[:digit:]]{3})\.([[:digit:]]{3}= )\.([[:digit:]]{4})', '(\1) \2-\3', 1); + regexp_replace=20 +---------------- + (512) 123-4567 +(1 row) + +SELECT regexp_replace('512.123.4567 612.123.4567', '([[:digit:]]{3})\.([= [:digit:]]{3})\.([[:digit:]]{4})', '(\1) \2-\3', 1, 0); + regexp_replace =20 +------------------------------- + (512) 123-4567 (612) 123-4567 +(1 row) + +SELECT regexp_replace('number your street,'||CHR(10)||' zipcode= town, FR', '( ){2,}', ' ', 1, 0); + regexp_replace =20 +--------------------- + number your street,+ + zipcode town, FR +(1 row) + +SELECT regexp_replace('number your street, zipcode town, FR', = '( ){2,}', ' ', 9); + regexp_replace =20 +-------------------------------------------- + number your street, zipcode town, FR +(1 row) + +SELECT regexp_replace('number your street, zipcode town, FR', = '( ){2,}', ' ', 9, 0); + regexp_replace =20 +---------------------------------------- + number your street, zipcode town, FR +(1 row) + +SELECT regexp_replace('number your street, zipcode town, FR', = '( ){2,}', ' ', 9, 2); + regexp_replace =20 +--------------------------------------------- + number your street, zipcode town, FR +(1 row) + +SELECT regexp_replace('number your street, zipcode town, FR', = '( ){2,}', ' ', 9, 2, 'm'); + regexp_replace =20 +--------------------------------------------- + number your street, zipcode town, FR +(1 row) + +SELECT regexp_replace('number your street, zipcode town, FR', = '([EURT]){2,}', '[\1]', 9, 1, 'i'); + regexp_replace =20 +------------------------------------------------- + number yo[r] street, zipcode town, FR +(1 row) + +SELECT regexp_replace('number your street, zipcode town, FR', = '([EURT]){2,}', '[\1]', 9, 2, 'i'); + regexp_replace =20 +---------------------------------------------- + number your s[t], zipcode town, FR +(1 row) + +SELECT regexp_replace ('A PostgreSQL function', 'A|e|i|o|u', 'X', 1, 2);= + regexp_replace =20 +----------------------- + A PXstgreSQL function +(1 row) + +SELECT regexp_replace ('A PostgreSQL function', 'a|e|i|o|u', 'X', 1, 0, = 'i'); + regexp_replace =20 +----------------------- + X PXstgrXSQL fXnctXXn +(1 row) + +SELECT regexp_replace ('A PostgreSQL function', 'a|e|i|o|u', 'X', 1, 1, = 'i'); + regexp_replace =20 +----------------------- + X PostgreSQL function +(1 row) + +SELECT regexp_replace ('A PostgreSQL function', 'a|e|i|o|u', 'X', 1, 2, = 'i'); + regexp_replace =20 +----------------------- + A PXstgreSQL function +(1 row) + +SELECT regexp_replace ('A PostgreSQL function', 'a|e|i|o|u', 'X', 1, 3, = 'i'); + regexp_replace =20 +----------------------- + A PostgrXSQL function +(1 row) + +SELECT regexp_replace ('A PostgreSQL function', 'a|e|i|o|u', 'X', 1, 9, = 'i'); + regexp_replace =20 +----------------------- + A PostgreSQL function +(1 row) + +SELECT regexp_replace ('A PostgreSQL function', 'A|e|i|o|u', 'X', 1, 9);= + regexp_replace =20 +----------------------- + A PostgreSQL function +(1 row) + +-- Invalid parameter values +SELECT regexp_replace ('A PostgreSQL function', 'a|e|i|o|u', 'X', -1, 0,= 'i'); +ERROR: invalid value for parameter "start_position": -1 +SELECT regexp_replace ('A PostgreSQL function', 'a|e|i|o|u', 'X', 1, -1,= 'i'); +ERROR: invalid value for parameter "occurrence": -1 +-- Modifier 'g' should not be taken in account, we have an occurrence to= replace +SELECT regexp_replace ('A PostgreSQL function', 'a|e|i|o|u', 'X', 1, 1, = 'g'); + regexp_replace =20 +----------------------- + A PXstgreSQL function +(1 row) + +-- Find groups of "word characters" (letters, numbers and underscore) en= ding with +-- "thy" in the string "healthy, wealthy, and wise" and replace them wit= h nothing. +SELECT regexp_replace('healthy, wealthy, and wise','\w+thy', '', 'g'); + regexp_replace=20 +---------------- + , , and wise +(1 row) + +SELECT regexp_replace('healthy, wealthy, and wise','\w+thy', '', 1, 0); + regexp_replace=20 +---------------- + , , and wise +(1 row) + +SELECT regexp_replace('healthy, wealthy, and wise','\w+thy', '', 1, 0, '= g'); + regexp_replace=20 +---------------- + , , and wise +(1 row) + +-- Find groups of word characters ending with "thy" and replace with "so= mething." +SELECT regexp_replace('healthy, wealthy, and wise','\w+thy', 'something'= , 'g'); + regexp_replace =20 +-------------------------------- + something, something, and wise +(1 row) + +-- Find groups of word characters ending with "thy" and replace with the= string +-- "something" starting at the third character in the string. +SELECT regexp_replace('healthy, wealthy, and wise','\w+thy', 'something'= , 3, 0); + regexp_replace =20 +---------------------------------- + hesomething, something, and wise +(1 row) + +-- Replace the second group of word characters ending with "thy" with "s= omething." +SELECT regexp_replace('healthy, wealthy, and wise','\w+thy', 'something'= , 1, 2); + regexp_replace =20 +------------------------------ + healthy, something, and wise +(1 row) + +-- Find groups of word characters ending with "thy" capturing the letter= s before +-- the "thy", and replace with the captured letters plus the letters "is= h."=20 +SELECT regexp_replace('healthy, wealthy, and wise','(\w+)thy', '\1ish', = 'g'); + regexp_replace =20 +---------------------------- + healish, wealish, and wise +(1 row) + +SELECT regexp_replace('healthy, wealthy, and wise','(\w+)thy', '\1ish', = 1, 0); + regexp_replace =20 +---------------------------- + healish, wealish, and wise +(1 row) + +-- Ambiguous use of option parameter with regexp_replace() +PREPARE rr AS SELECT regexp_replace('healthy, wealthy, and wise','(\w+)t= hy', '\1ish', $1); +EXECUTE rr(1); +ERROR: ambiguous use of the option parameter in regex_replace(), value:= 1 +HINT: you might set the occurrence parameter to force the use of the ex= tended form of regex_replace() +DEALLOCATE rr; -- change NULL-display back \pset null '' -- E021-11 position expression diff --git a/src/test/regress/sql/strings.sql b/src/test/regress/sql/stri= ngs.sql index 2c502534c2..f50131896f 100644 --- a/src/test/regress/sql/strings.sql +++ b/src/test/regress/sql/strings.sql @@ -255,6 +255,202 @@ SELECT regexp_split_to_array('thE QUick bROWn FOx j= UMPs 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 dO= G', 'e', 'g'); =20 +-- regexp_like tests +SELECT regexp_like('a'||CHR(10)||'d', 'a.d'); +SELECT regexp_like('a'||CHR(10)||'d', 'a.d', 'm'); +SELECT regexp_like('a'||CHR(10)||'d', 'a.d', 'n'); +SELECT regexp_like('Steven', '^Ste(v|ph)en$'); +SELECT regexp_like('foo' || chr(10) || 'bar' || chr(10) || 'bequq' || ch= r(10) || 'baz', '^bar'); +SELECT regexp_like('foo' || chr(10) || 'bar' || chr(10) || 'bequq' || ch= r(10) || 'baz', 'bar'); +SELECT regexp_like('foo' || chr(10) || 'bar' || chr(10) || 'bequq' || ch= r(10) || 'baz', '^bar', 'm'); +SELECT regexp_like('foo' || chr(10) || 'bar' || chr(10) || 'bequq' || ch= r(10) || 'baz', '^bar', 'n'); +SELECT regexp_like('GREEN', '([aeiou])\1'); +SELECT regexp_like('GREEN', '([aeiou])\1', 'i'); +SELECT regexp_like('ORANGE' || chr(10) || 'GREEN', '([aeiou])\1', 'i'); +SELECT regexp_like('ORANGE' || chr(10) || 'GREEN', '^..([aeiou])\1', 'i'= ); +SELECT regexp_like('ORANGE' || chr(10) || 'GREEN', '([aeiou])\1', 'in');= +SELECT regexp_like('ORANGE' || chr(10) || 'GREEN', '^..([aeiou])\1', 'in= '); +SELECT regexp_like('ORANGE' || chr(10) || 'GREEN', '^..([aeiou])\1', 'im= '); +SELECT REGEXP_LIKE('abc', 'a b c'); +SELECT REGEXP_LIKE('abc', 'a b c','x'); + +-- 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 +-- Count the number of occurrences of the substring an in the string. +SELECT regexp_count('a man, a plan, a canal: Panama', 'an'); +-- Find the number of occurrences of the substring an in the string star= ting with the fifth character. +SELECT regexp_count('a man, a plan, a canal: Panama', 'an',5); +-- Find the number of occurrences of a substring containing a lower-case= character +-- followed by an. In the first example, do not use a modifier. In the s= econd example, +-- use the i modifier to force the regular expression to ignore case. +SELECT regexp_count('a man, a plan, a canal: Panama', '[a-z]an'); +SELECT regexp_count('a man, a plan, a canal: Panama', '[a-z]an', 1, 'i')= ; + +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@e= xample.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 fir= st 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 fi= rst 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 fi= rst 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 g= roup +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_ema= il" 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) FR= OM 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) FR= OM 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);= + +-- Find the first occurrence of a sequence of letters starting with the = letter e +-- and ending with the letter y in the phrase "easy come, easy go." +SELECT regexp_instr('easy come, easy go','e\w*y'); +-- Find the first sequence of letters starting with the letter e and end= ing with +-- the letter y in the string "easy come, easy go" starting at the secon= d character +SELECT regexp_instr('easy come, easy go','e\w*y',2); +-- Find the second sequence of letters starting with the letter e and en= ding with +-- the letter y in the string "easy come, easy go" starting at the first= character. +SELECT regexp_instr('easy come, easy go','e\w*y',1,2); +-- Find the position of the first character after the first whitespace i= n the string "easy come, easy go." +SELECT regexp_instr('easy come, easy go','\s',1,1,1); +-- Find the position of the start of the third word in a string by captu= ring each +-- word as a subexpression, and returning the third subexpression's star= t position. +SELECT regexp_instr('one two three','(\w+)\s+(\w+)\s+(\w+)', 1,1,0,'',3)= ; + +-- 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); +-- Select the first substring of letters that end with "thy." +SELECT regexp_substr('healthy, wealthy, and wise','\w+thy'); +-- Select the first substring of letters that ends with "thy" starting a= t the second character in the string. +SELECT regexp_substr('healthy, wealthy, and wise','\w+thy',2); +-- Return the contents of the third captured subexpression, which captur= es the third word in the string. +SELECT regexp_substr('one two three', '(\w+)\s+(\w+)\s+(\w+)', 1, 1, '',= 3); + +DROP TABLE IF EXISTS regexp_temp; + +-- Regression tests for extended regexp_replace() function with start po= sition and occurrence +SELECT regexp_replace('512.123.4567', '([[:digit:]]{3})\.([[:digit:]]{3}= )\.([[:digit:]]{4})', '(\1) \2-\3', 1); +SELECT regexp_replace('512.123.4567 612.123.4567', '([[:digit:]]{3})\.([= [:digit:]]{3})\.([[:digit:]]{4})', '(\1) \2-\3', 1, 0); +SELECT regexp_replace('number your street,'||CHR(10)||' zipcode= town, FR', '( ){2,}', ' ', 1, 0); +SELECT regexp_replace('number your street, zipcode town, FR', = '( ){2,}', ' ', 9); +SELECT regexp_replace('number your street, zipcode town, FR', = '( ){2,}', ' ', 9, 0); +SELECT regexp_replace('number your street, zipcode town, FR', = '( ){2,}', ' ', 9, 2); +SELECT regexp_replace('number your street, zipcode town, FR', = '( ){2,}', ' ', 9, 2, 'm'); +SELECT regexp_replace('number your street, zipcode town, FR', = '([EURT]){2,}', '[\1]', 9, 1, 'i'); +SELECT regexp_replace('number your street, zipcode town, FR', = '([EURT]){2,}', '[\1]', 9, 2, 'i'); +SELECT regexp_replace ('A PostgreSQL function', 'A|e|i|o|u', 'X', 1, 2);= +SELECT regexp_replace ('A PostgreSQL function', 'a|e|i|o|u', 'X', 1, 0, = 'i'); +SELECT regexp_replace ('A PostgreSQL function', 'a|e|i|o|u', 'X', 1, 1, = 'i'); +SELECT regexp_replace ('A PostgreSQL function', 'a|e|i|o|u', 'X', 1, 2, = 'i'); +SELECT regexp_replace ('A PostgreSQL function', 'a|e|i|o|u', 'X', 1, 3, = 'i'); +SELECT regexp_replace ('A PostgreSQL function', 'a|e|i|o|u', 'X', 1, 9, = 'i'); +SELECT regexp_replace ('A PostgreSQL function', 'A|e|i|o|u', 'X', 1, 9);= +-- Invalid parameter values +SELECT regexp_replace ('A PostgreSQL function', 'a|e|i|o|u', 'X', -1, 0,= 'i'); +SELECT regexp_replace ('A PostgreSQL function', 'a|e|i|o|u', 'X', 1, -1,= 'i'); +-- Modifier 'g' should not be taken in account, we have an occurrence to= replace +SELECT regexp_replace ('A PostgreSQL function', 'a|e|i|o|u', 'X', 1, 1, = 'g'); +-- Find groups of "word characters" (letters, numbers and underscore) en= ding with +-- "thy" in the string "healthy, wealthy, and wise" and replace them wit= h nothing. +SELECT regexp_replace('healthy, wealthy, and wise','\w+thy', '', 'g'); +SELECT regexp_replace('healthy, wealthy, and wise','\w+thy', '', 1, 0); +SELECT regexp_replace('healthy, wealthy, and wise','\w+thy', '', 1, 0, '= g'); +-- Find groups of word characters ending with "thy" and replace with "so= mething." +SELECT regexp_replace('healthy, wealthy, and wise','\w+thy', 'something'= , 'g'); +-- Find groups of word characters ending with "thy" and replace with the= string +-- "something" starting at the third character in the string. +SELECT regexp_replace('healthy, wealthy, and wise','\w+thy', 'something'= , 3, 0); +-- Replace the second group of word characters ending with "thy" with "s= omething." +SELECT regexp_replace('healthy, wealthy, and wise','\w+thy', 'something'= , 1, 2); +-- Find groups of word characters ending with "thy" capturing the letter= s before +-- the "thy", and replace with the captured letters plus the letters "is= h."=20 +SELECT regexp_replace('healthy, wealthy, and wise','(\w+)thy', '\1ish', = 'g'); +SELECT regexp_replace('healthy, wealthy, and wise','(\w+)thy', '\1ish', = 1, 0); +-- Ambiguous use of option parameter with regexp_replace() +PREPARE rr AS SELECT regexp_replace('healthy, wealthy, and wise','(\w+)t= hy', '\1ish', $1); +EXECUTE rr(1); +DEALLOCATE rr; + -- change NULL-display back \pset null '' =20 --------------7953F524A7176BA9DC9ED9B3--