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 1lHNc5-00049g-1a for pgsql-hackers@arkaria.postgresql.org; Wed, 03 Mar 2021 09:16:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lHNc3-0005Aa-UL for pgsql-hackers@arkaria.postgresql.org; Wed, 03 Mar 2021 09:16:11 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lHNc3-0005AT-AF for pgsql-hackers@lists.postgresql.org; Wed, 03 Mar 2021 09:16:11 +0000 Received: from 10.mo178.mail-out.ovh.net ([46.105.76.150]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lHNbw-0007ei-04 for pgsql-hackers@postgresql.org; Wed, 03 Mar 2021 09:16:10 +0000 Received: from player158.ha.ovh.net (unknown [10.109.146.211]) by mo178.mail-out.ovh.net (Postfix) with ESMTP id 8BD2CC693E for ; Wed, 3 Mar 2021 10:16:00 +0100 (CET) Received: from darold.net (lfbn-lyo-1-666-87.w90-119.abo.wanadoo.fr [90.119.236.87]) (Authenticated sender: gilles@darold.net) by player158.ha.ovh.net (Postfix) with ESMTPSA id D3C1D1B9750D3 for ; Wed, 3 Mar 2021 09:15:58 +0000 (UTC) Authentication-Results: garm.ovh; auth=pass (GARM-97G00275cc0a8f-a62a-4f38-8ae2-b8fa9cface3b, 0D00BC6CBF8509F341B155B793128C79F991543F) smtp.auth=gilles@darold.net X-OVh-ClientIp: 90.119.236.87 To: PostgreSQL Hackers From: Gilles Darold Subject: [PATCH] proposal for regexp_count, regexp_instr, regexp_substr and regexp_replace Message-ID: Date: Wed, 3 Mar 2021 10:15:57 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.7.1 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Content-Language: en-US X-Ovh-Tracer-Id: 6039327102865181503 X-VR-SPAMSTATE: OK X-VR-SPAMSCORE: -100 X-VR-SPAMCAUSE: gggruggvucftvghtrhhoucdtuddrgeduledruddtvddgtdefucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuqfggjfdpvefjgfevmfevgfenuceurghilhhouhhtmecuhedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujfgurhepvffhuffkffgfgggtgfesthhqredttdefjeenucfhrhhomhepifhilhhlvghsucffrghrohhlugcuoehgihhllhgvshesuggrrhholhgurdhnvghtqeenucggtffrrghtthgvrhhnpefhvdehtefhvedtffejudegveejtefghfeltddvjefgkedtkeevuddujedtieeujeenucffohhmrghinhepohhrrggtlhgvrdgtohhmpdhisghmrdgtohhmpdgvnhhtvghrphhrihhsvggusgdrtghomhenucfkpheptddrtddrtddrtddpledtrdduudelrddvfeeirdekjeenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhhouggvpehsmhhtphdqohhuthdphhgvlhhopehplhgrhigvrhduheekrdhhrgdrohhvhhdrnhgvthdpihhnvghtpedtrddtrddtrddtpdhmrghilhhfrhhomhepghhilhhlvghssegurghrohhlugdrnhgvthdprhgtphhtthhopehpghhsqhhlqdhhrggtkhgvrhhssehpohhsthhgrhgvshhqlhdrohhrgh List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Oracle: https://docs.oracle.com/en/database/oracle/oracle-database/18/adfns/regex= p.html#GUID-F14733F3-B943-4BAD-8489-F9704986386B IBM: https://www.ibm.com/support/producthub/db2/docs/content/SSEPGG_11.5.0/com= =2Eibm.db2.luw.sql.ref.doc/doc/r0061494.html?pos=3D2 Z/OS: https://www.ibm.com/support/knowledgecenter/SSEPEK_12.0.0/sqlref/src/tpc/= db2z_bif_regexplike.html EDB: https://www.enterprisedb.com/edb-docs/d/edb-postgres-advanced-server/refe= rence/database-compatibility-for-oracle-developers-reference-guide/9.6/Da= tabase_Compatibility_for_Oracle_Developers_Reference_Guide.1.098.html Hi, I would like to suggest adding the $subject functions to PostgreSQL. We can do lot of things using regexp_matches() and regexp_replace() but some time it consist on building complex queries that these functions can greatly simplify. Look like all RDBMS that embedded a regexp engine implement these functions (Oracle, DB2, MySQL, etc) but I don't know if they are part of the SQL standard. Probably using regexp_matches() can be enough even if it generates more complex statements but having these functions in PostgreSQL could be useful for users and code coming from theses RDBMS. =C2=A0 - REGEXP_COUNT( string text, pattern text, [, position int] [, fla= gs text ] ) -> integer =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 Return the number of times a patter= n occurs in a source string after a certain position, default from beginning. =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 It can be implemented in PostgreSQL= as a subquery using: =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 SELECT count(*) = FROM regexp_matches('A1B2C3', '[A-Z][0-9]', 'g'); -> 3 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 To support positioning we have to u= se substr(), for example starting at position 2: =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 SELECT count(*) = FROM regexp_matches(substr('A1B2C3', 2), '[A-Z][0-9]'); -> 2 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 With regexp_count() we can simply u= se it like this: =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 SELECT regexp_co= unt('A1B2C3', '[A-Z][0-9]'); -> 3 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 SELECT regexp_co= unt('A1B2C3', '[A-Z][0-9]', 2); -> 2 =C2=A0 - REGEXP_INSTR( string text, pattern text, [, position int] [, occurrence int] [, return_opt int ] [, flags text ] [, group int] ) -> integer =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 Return the position in a string for= a regular expression pattern. It returns an integer indicating the beginning or ending position of the matched substring, depending on the value of the return_opt argument (default beginning). If no match is found, then the function returns 0. =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 * position: indi= cates the character where the search should begin. =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 * occurrence: in= dicates which occurrence of pattern found in string should be search. =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 * return_opt: 0 = mean returns the position of the first character of the occurrence, 1 mean returns the position of the character following the occurrence. =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 * flags: regular= expression modifiers. =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 * group: indicat= es which subexpression in pattern is the target of the function. =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 Example: =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 SELECT regexp_in= str('1234567890', '(123)(4(56)(78))', 1, 1, 0, 'i', 4); -> 7 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 to obtain a PostgreSQL equivalent: =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 SELECT position(= (SELECT (regexp_matches('1234567890', '(123)(4(56)(78))', 'ig'))[4] offset 0 limit 1) IN '1234567890'); =C2=A0 - REGEXP_SUBSTR( string text, pattern text, [, position int] [, occurrence int] [, flags text ] [, group int] ) -> text =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 It is similar to regexp_instr(), bu= t instead of returning the position of the substring, it returns the substring itself. =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 Example: =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 SELECT regexp_su= bstr('500 gilles''s street, 38000 Grenoble, FR', ',[^,]+,'); -> , 38000 Grenoble, =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 or with a more complex extraction: =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 SELECT regexp_su= bstr('1234567890', '(123)(4(56)(78))', 1, 1, 'i', 4); -> 78 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 SELECT regexp_su= bstr('1234567890 1234557890', '(123)(4(5[56])(78))', 1, 2, 'i', 3); -> 55 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 To obtain the same result for the l= ast example we have to use: =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 SELECT (SELECT *= FROM regexp_matches('1234567890 1234557890', '(123)(4(5[56])(78))',=C2=A0 'g') offset 1 limit 2)[3]; I have not implemented the regexp_like() function, it is quite similar than the ~ and ~* operators except that it can also support other modifiers than 'i'. I can implement it easily and add it to the patch if we want to supports all those common functions. =C2=A0 - REGEXP_LIKE( string text, pattern text, [, flags text ] ) -> boo= lean =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 Similar to the LIKE condition, exce= pt that it performs regular expression matching instead of the simple pattern matching performed by LIKE. =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 Example: =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 SELECT * FROM t1= WHERE regexp_like(col1, '^d$', 'm'); =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 to obtain a PostgreSQL equivalent: =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 SELECT * FROM t1= WHERE regexp_match (col1, '^d$', 'm' ) IS NOT NULL; There is also a possible extension to regexp_replace() that I have not implemented yet because it need more work than the previous functions. =C2=A0 - REGEXP_REPLACE( string text, pattern text, replace_string text, = [, position int] [, occurrence int] [, flags text ] ) =C2=A0=C2=A0=C2=A0 Extend PostgreSQL regexp_replace() by adding position = and occurrence capabilities. The patch is ready for testing with documentation and regression tests. Best regards, --=20 Gilles Darold LzLabs GmbH