Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1s9GR3-0011bM-3X for pgsql-hackers@arkaria.postgresql.org; Tue, 21 May 2024 03:45:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1s9GQ4-007KBb-H5 for pgsql-hackers@arkaria.postgresql.org; Tue, 21 May 2024 03:44:08 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1s9GQ4-007KBS-7D for pgsql-hackers@lists.postgresql.org; Tue, 21 May 2024 03:44:08 +0000 Received: from mail-oo1-xc2d.google.com ([2607:f8b0:4864:20::c2d]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1s9GQ1-001ELw-Id for pgsql-hackers@lists.postgresql.org; Tue, 21 May 2024 03:44:06 +0000 Received: by mail-oo1-xc2d.google.com with SMTP id 006d021491bc7-5b277e17e15so2898556eaf.2 for ; Mon, 20 May 2024 20:44:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716263045; x=1716867845; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:references:in-reply-to :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=YmAMlJa0uzMYULGA55sRgU/BZd5sw9aCyfmvb2hmWCI=; b=UuYynCZCmqr1osCem8/NHT2Gp/3iHpBRDdfVvQHBsAsTfWXZINqARj5tJoDb9NBz/0 kFSwcEAYE5c38iI4eM5TWoFWL7IcRa6VEMBhd7RfkT7vglsf5BOI1MGXbOdswZPsJyd4 1jSRj0FHsAgk3Jyd6Mi/bLdrec6Jv0m+6xhz8DQcJZGxye6PkVo4Jtum6JTLYzVZrGSb pu51TtvKebzljCbaFTIQNlute0230WV8C28dlFzsIbTpDBcDkEQ+ZlnQvbxQahHU11uC PdKA3WTDX9eOhWA/fhSwpGaSvEZLe1tusSDQzi4G3ycbw/6mAQp29Q4jqX0OUMWKw02p 7OAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716263045; x=1716867845; h=cc:to:subject:message-id:date:from:references:in-reply-to :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=YmAMlJa0uzMYULGA55sRgU/BZd5sw9aCyfmvb2hmWCI=; b=c7E+0RT0fLawwAOF6j6Hpc/N8kaCMdzVzktyATG5rBoak8qi3xOrj/u7/kYdv9y/F6 lEzo7dw0SYO2B2oCaL2lL5OyErch7uztCwgK8udQAanQxZpthNU5X1pPiY4msensmjfV 2d2/3BzuVDQ6O9U2L277aUViPNiue/NH9s2FfGPs2pqcA/yilrcT7s6BwxjX0i15RNrW CLOln1aG9MTO5b2Izuh35pc1bGrEhJQx6xLvpEwO7/4OILSQMzXrq0un/Y6ke+4eu1bw 2x5yyErAh7ovoZh7UkkkW2MfLrmpEOO3bfuJB9luEaxq2OZKKVUu/ZBaiREn33kr6Vhx PmoQ== X-Gm-Message-State: AOJu0YyIJUywUZ8y57GI6VrjGF3IOMckBJMRvbmn3SemgkZl8D6WeMbA 4jER1fn+WJ0ws7cDPm6o+U3aSeWDhfhc5mMJSaw0V4QlwwbimLtoGwjRZvoYVT43oK8JzHwx0ay 1Zmpk0bafsLPIqIsPCyBzvjoFlhM= X-Google-Smtp-Source: AGHT+IE+h2s8Li8SMfD9bEJBBtN+OtkkTKTykNzrEyDCJP0yVgH60bFwzfec4YfR5zkGGzC/Ci4D/LQSrz4xSto0Ts8= X-Received: by 2002:a05:6820:1c97:b0:5b3:289:9e38 with SMTP id 006d021491bc7-5b302899feemr17866869eaf.5.1716263044644; Mon, 20 May 2024 20:44:04 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:ac9:7f13:0:b0:52c:cea1:1841 with HTTP; Mon, 20 May 2024 20:44:04 -0700 (PDT) In-Reply-To: References: From: "David G. Johnston" Date: Mon, 20 May 2024 20:44:04 -0700 Message-ID: Subject: Re: doc regexp_replace replacement string \n does not explained properly To: jian he Cc: PostgreSQL Hackers Content-Type: multipart/alternative; boundary="0000000000001e0a9d0618eea18a" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000001e0a9d0618eea18a Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Monday, May 20, 2024, jian he wrote: > hi. > > https://www.postgresql.org/docs/current/functions- > matching.html#FUNCTIONS-POSIX-REGEXP > > > If there is a match, > the source string is returned with the replacement string substituted > for the matching substring. > This happens regardless of the presence of parentheses. > > The replacement string can contain \n, > where n is 1 through 9, to indicate that the source substring matching > the n'th parenthesized subexpression of the pattern should be > inserted, and it can contain \& to indicate that the substring > matching the entire pattern should be inserted. Then if the replacement text contains =E2=80=9C\n=E2=80=9D expressions tho= se are replaced with text captured from the corresponding parentheses group. > << > i think it explained example like: > SELECT regexp_replace('foobarbaz', 'b(..)', 'X\1Y', 'g'); global - find two matches to process. foobarbaz fooX\1YX\1Y fooXarYXazY > > but it does not seem to explain cases like: > SELECT regexp_replace('foobarbaz', 'b(..)', 'X\2Y', 'g'); > > foobarbaz fooX\2YX\2Y fooX{empty string, no second capture group}YX{empty}Y fooXYXY The docs are correct, though I suppose being explicit that a missing capture group results in an empty string substitution instead of an error is probably warranted. David J. --0000000000001e0a9d0618eea18a Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Monday, May 20, 2024, jian he <jian.universality@gmail.com> wrote:
hi.

https://www.postgresql.org/d= ocs/current/functions-matching.html#FUNCTIONS-POSIX-REGEXP

=C2=A0If there is a match,
the source string is returned with the replacement string substituted
for the matching substring.

=

This happens regardless of the presence of= parentheses.



=C2=A0The replacement string can contain \n,
where n is 1 through 9, to indicate that the source substring matching
the n'th parenthesized subexpression of the pattern should be
inserted, and it can contain \& to indicate that the substring
matching the entire pattern should be inserted.

=
=C2=A0Then if the replacement text contains =E2=80=9C\n=E2=80=9D expre= ssions those are replaced with text captured from the corresponding parenth= eses group.


<<
i think it explained example like:
SELECT regexp_replace('foobarbaz', 'b(..)', 'X\1Y',= 'g');

global - find two matches to= process.

foobarbaz
fooX\1YX\1Y
fooXarYXazY
=C2=A0

but it does not seem to explain cases like:
SELECT regexp_replace('foobarbaz', 'b(..)', 'X\2Y',= 'g');


foobarbaz
= fooX\2YX\2Y
fooX{empty string, no second capture group}YX{empty}Y=
fooXYXY

The docs are correct, though I = suppose being explicit that a missing capture group results in an empty str= ing substitution instead of an error is probably warranted.

<= /div>
David J.

--0000000000001e0a9d0618eea18a--