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 1nFi5P-0000tZ-Gi for pgsql-general@arkaria.postgresql.org; Thu, 03 Feb 2022 19:48:07 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nFi5O-0006YV-6d for pgsql-general@arkaria.postgresql.org; Thu, 03 Feb 2022 19:48:06 +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 1nFi5N-0006YL-TG for pgsql-general@lists.postgresql.org; Thu, 03 Feb 2022 19:48:05 +0000 Received: from mail.hjp.at ([212.17.106.138] helo=rorschach.hjp.at) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nFi5K-0005BW-V0 for pgsql-general@lists.postgresql.org; Thu, 03 Feb 2022 19:48:05 +0000 Received: by rorschach.hjp.at (Postfix, from userid 1000) id 32ED963BD; Thu, 3 Feb 2022 20:48:00 +0100 (CET) Date: Thu, 3 Feb 2022 20:48:00 +0100 From: "Peter J. Holzer" To: pgsql-general@lists.postgresql.org Subject: Re: Regular Expression For Duplicate Words Message-ID: <20220203194800.pg3bzm33dzzdpqfb@hjp.at> Mail-Followup-To: pgsql-general@lists.postgresql.org References: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="b5qje3lmtoj5k567" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --b5qje3lmtoj5k567 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On 2022-02-02 08:00:00 +0000, Shaozhong SHI wrote: > regex - Regular Expression For Duplicate Words - Stack Overflow >=20 > Is there any example in Postgres? It's pretty much the same as with other regexp dialects: User word boundaries and a word character class to match any word and then use a backreference to match a duplicate word. All the building blocks are described on https://www.postgresql.org/docs/current/functions-matching.html#FUNCTIONS-P= OSIX-REGEXP and except for [[:<:]] and [[:>:]] for the word boundaries, they are also pretty standard. So [[:<:]] start of word ([[:alpha:]]+) one or more alphabetic characters in a capturing group [[:>:]] end of word \W+ one or more non-word characters [[:<:]] start of word \1 the content of the first (and only) capturing group [[:>:]] end of word All together: select * from t where t ~ '[[:<:]]([[:alpha:]]+)[[:>:]]\W[[:<:]]\1[[:>:]]'; hp --=20 _ | Peter J. Holzer | Story must make more sense than reality. |_|_) | | | | | hjp@hjp.at | -- Charles Stross, "Creative writing __/ | http://www.hjp.at/ | challenge!" --b5qje3lmtoj5k567 Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAmH8MWsACgkQ8g5IURL+ KF3aTQ//fk5yztVy9wxwtcy9kWr6h/wXFvUNgXX/r61JzxDv/rqWCFL9VrcMrDEP gxbYFUUYYbfl2pA3CQt5e1IvZAajVpAeV77onoccUdb7bHLf5D6ezWwN4QVV0PlN bh1eWf5gxmEpAzubZGkewo3MtVN6hXG933Oe2MzYIbceiRdYwdbbFVKb3RxQXUMx Ts2wpYrsSSTQ+JG+vKZy0jleqbqyo7ih1OGi4z3fJGB+pioe2E1F1bEeasNClvub SlQzNOpD35xwG3R2sMVsJY8ZOK3WT/hza4A2IiCXnEHHm2e6eRZK0ShAKirQ/lvs cb2NKfHiGOpeYhq/mKBzJfNtXC2XX0vd2cEqucIngTRg65PNKIhfeIZnbqgmum5f W+tFjH6TS01W3SLmWOkvFaf5x8Y0LBwXittl18iwDoXGhD1wC8luexoBmNsi396j vvSBIZ6s+j8Ko+gMZ72rUZ4tQVtFnxW0r3SjHdTR20aMoKOH7uWMFtTmpm4/nLlm 5NGYviTNs2PRVnDYICDHXr9xhKtiu68d9xaOvOL/NwhhjcHCEi3u6aZ6nqagectj pnbQqZdFv0PxvZjRj2Y25CyEdPMhLPeYwEhPUtOvXQiEYlUjg7L4P21V6GjSofWW 1kH5tcUk22Sz86mlzTiZggjSN8JRaeAQY4OzKxxATSrlPeWey74= =4PyS -----END PGP SIGNATURE----- --b5qje3lmtoj5k567--