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 1pMPGN-0006tv-AN for pgsql-bugs@arkaria.postgresql.org; Mon, 30 Jan 2023 08:11:39 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pMPGK-0005h3-NU for pgsql-bugs@arkaria.postgresql.org; Mon, 30 Jan 2023 08:11:36 +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 1pMPGK-0005gu-4V for pgsql-bugs@lists.postgresql.org; Mon, 30 Jan 2023 08:11:36 +0000 Received: from sonic302-19.consmr.mail.ir2.yahoo.com ([87.248.110.82]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pMPGH-00069d-2s for pgsql-bugs@lists.postgresql.org; Mon, 30 Jan 2023 08:11:35 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1675066290; bh=Sp6eTEazJqKm2vcxorX+OAigPmj9RnajBnajKHhZ3zM=; h=Date:From:To:Cc:In-Reply-To:References:Subject:From:Subject:Reply-To; b=Nry9U6hUm6Ibvm9XDGbo7WqNjI36rqSgotLTx1xG3Fr1LXYKVS13ibELyP6fqI2mWepUBf+72Ib00aUJCqkYvSIe21xytriDOv8K5Y1yHBHsbv/b5WJHsOGLQ8EInG5bBNVeyJ4T9IklEFs9aMxV20H4Gv69mfItkgxclTWuPSRgFkT0k+ig+ZN0drbnQLC/b3af6BTWT6lLrUbELqKP4FlV3bFLnDmPhM7jlz2QSxBaolnVtBOC+aYiDsiU+3kIkTUn2A5AVyHcypqQXCn9kAOjUWJV6lBcvxdCmDDm6yexnUDSfBBFiubtibOixPbFQDvyWyrRCgWXqAvBf56rUg== X-SONIC-DKIM-SIGN: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1675066290; bh=APvdbGa4qmvGaBcbVbiV8aSqaTlyFfZN7DATmGMi1ub=; h=X-Sonic-MF:Date:From:To:Subject:From:Subject; b=Ws7IV+nnWNKM6M6/2cdxIa18ULe9xo/HFv7Z4D4MU3yhrilBswbD6dxNXCxxnaIpM89y4p948bmQFMo0hVSEQzuH/i1vH103kj0HbcUpGKLGF0V6LvYXU7c4H8vZy5c+nfe+Gz3QEe2Yll4/W9rZNdiXrL7MQxGOzNzfQivMQ33FaUq9lto/6RZId0+Jd3Y5klhYMAMBr7XtuLI5afm38JyvTJwSsKkC08MUNwhbSUnBTpfKSSj5BpgIPTrLcbFaj9xs/TIaBV04qQsP4m3VMp2hCD5n/j154DqZqsO8pk9bP0vJZsbYOFXHNa5+V52LA7OcSLZdmYYzwa/KJLcnFw== X-YMail-OSG: o8_ORK8VM1l7IV5s0Me4F935hMSwXG9Xsk59PFBIzAkS1dhTYPe4DZwNQE2mTSq Yubvic_dsw6.ufmo1H4_spnwzCKuXUq7prcl9XYoznEGJvOxinraV38sHybNeBLXU2SCY8QlM8VZ b74qg1_XnpscPyLDzXNXYrytMBBhOUCnk.APzEgCTnqzTJBcUnIhkcOTajLU4JAqHPhrp3LdOr0b 3Ay6XXelJ0s1EDRcc7yGJCtD8vzm2kGLm7Rejd71ymX5wkfYP3AaLkrZcWSj4u3xSRO9pfRTAyWx NO4Kcf9tS8nY13Pxz.uckgkEuIN5IQZW0cdnlspeXhUnqSCdclyZ0lV2rUC0TpnKARuwFBaxIi73 wGmKCSr9wGN0mJzuEraPOuj2SGJqjFrj5YLW1uJ91YC2Iop4eNSsh6pGs0qC.mJP8jSubrUdqFKu AaiT7jdPlVxyGikFo.qWO_q1sy.CPxZGpn4__EICEgNHuX3kQG16O0YMKOaWHofWYGmDl7tdmPxs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9Zdg4HRZ_ym c_vbp2a4oSuW9WAQBl_MI58h8BUJJ_7yyJW9.cfZHNCHTK45YSV69179uen2FqQ01umf2Lkj7pFu 9qYYcRtV90p1Oj_m03lrap9PEX6zX17Q7MUzvbrZr8Hijq5OVUzf.YSEJhxSPQNCloaYlBHAa_Dz 9.EQqf7uN.27D_1wwvSO8PPqgBYA83klVh_C39qGwSToRwxISedwUQIJOkEM2PQYCOk8iz.psRoR aBVdASzZ2QEShKJqtaZDGg9eoLimAMqox3e.0UXCjv0tdQqFc9ZXVqrg8ohCmcyG.HCP2HI70.hZ T_plxTqa0X169HIXMF1kuaMMtd6Mzlvz.IIcSLVlLfOomZeTdRn0aPy4.WQssPiB..q3bt4JfWbt yZEWAcAaYFa6Na21r8p4eqPMel65rutdp8rGQrAv8ROaDeijxbls3Yms_Yo8j34o4CMrhT..ldtg xELn4rZeO..lbllFKVJzzmt9QPkCRC_YLFImu_02WoFJjQvA55JmGM3_QXYgg4DZ.H_qvivN7Or2 BpyzOu_gYBawTab9dovuDOMs9Re.k3RCJ68fA17ry7BvyJgmtF_a2RQKSccFLL4OE72fF96Xok.w tPiA0ymvxtqE_1kU1Ft0Aa2ZdH_UKCCFwTpiQJ0cAx7bvNbddhy3Wdqn3S.JqZMbK._wg6HUBOE0 - X-Sonic-MF: Received: from sonic.gate.mail.ne1.yahoo.com by sonic302.consmr.mail.ir2.yahoo.com with HTTP; Mon, 30 Jan 2023 08:11:30 +0000 Date: Mon, 30 Jan 2023 08:11:26 +0000 (UTC) From: Kosio Dimitrov To: "depesz@depesz.com" , Tom Lane Cc: "pgsql-bugs@lists.postgresql.org" Message-ID: <36522775.1746773.1675066286328@mail.yahoo.com> In-Reply-To: <3334493.1674835493@sss.pgh.pa.us> References: <17761-5a78c34533f3ab72@postgresql.org> <3334493.1674835493@sss.pgh.pa.us> Subject: Re: BUG #17761: Questionable regular expression behavior MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_1746772_203074880.1675066286327" X-Mailer: WebService/1.1.21123 YMailNorrin Content-Length: 5248 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ------=_Part_1746772_203074880.1675066286327 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Yes, greediness seems to be the case,One other solution I found to make it= greedy is to add '.*$' at the end:'(a).*?([1-9]\d*)\s*x\s*([1-9]\d*)(?:\s*= x\s*([1-9]\d*))?.*$' =D0=92 =D0=BF=D0=B5=D1=82=D1=8A=D0=BA, 27 =D1=8F=D0=BD=D1=83=D0=B0=D1= =80=D0=B8 2023 =D0=B3., 18:05:01 =D1=87. =D0=93=D1=80=D0=B8=D0=BD=D1=83=D0= =B8=D1=87+2, Tom Lane =D0=BD=D0=B0=D0=BF=D0=B8=D1=81=D0= =B0: =20 =20 hubert depesz lubaczewski writes: > On Fri, Jan 27, 2023 at 09:27:35AM +0000, PG Bug reporting form wrote: >> Executing: >> select regexp_matches('a 1x1250x2500', >> '(a).*?([1-9]\d*)\s*x\s*([1-9]\d*)(?:\s*x\s*([1-9]\d*))?'); >> returns: {a,1,1,NULL} >> while executing: >> select regexp_matches('a 1x1250x2500', >> '(a|b).*?([1-9]\d*)\s*x\s*([1-9]\d*)(?:\s*x\s*([1-9]\d*))?'); >> returns: {a,1,1250,2500} >>=20 >> Shouldn't both results be equal? > The problem is, afair, that there is some state in pg's regexp engine > that makes greedy/ungreedy decision once per regexp. Yeah.=C2=A0 Without having traced through it, I'm fairly sure that in the first case, we have "(a)" which has no greediness, then ".*?" which is non-greedy, and then that determines the overall greediness as non-greedy, so it goes for the shortest overall match not the longest. In the second case, "(a|b)" is greedy because anything involving "|" is greedy, so we immediately decide we'll be greedy overall. The fine manual explains how you can force greediness or non-greediness when the engine's default rules for that don't do what you want. =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 regards, tom lane =20 ------=_Part_1746772_203074880.1675066286327 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Yes, greediness seems to be = the case,
One other solution I found to make it greedy is to add '.= *$' at the end:
'(a).*?([= 1-9]\d*)\s*x\s*([1-9]\d*)(?:\s*x\s*([1-9]\d*))?.*$'
=20
=20
=D0=92 =D0=BF=D0=B5=D1=82=D1=8A=D0=BA, 27 =D1=8F=D0=BD= =D1=83=D0=B0=D1=80=D0=B8 2023 =D0=B3., 18:05:01 =D1=87. =D0=93=D1=80=D0=B8= =D0=BD=D1=83=D0=B8=D1=87+2, Tom Lane <tgl@sss.pgh.pa.us> =D0=BD=D0=B0= =D0=BF=D0=B8=D1=81=D0=B0:


hubert depesz lubaczewski <depesz@depesz.com> writes:

> On Fri, Jan 27, 2023 at 09:27:35AM +000= 0, PG Bug reporting form wrote:
>> Executing:
>> select regexp_matches('a 1x1250x2500',
>> '(a).*?([1-9]\d*)\s*x\s*([1-9]\d*)(?:\s*x\s*([1-9]\d*))?');>> returns: {a,1,1,NULL}
>> w= hile executing:
>> select regexp_matches('a 1x1250x= 2500',
>> '(a|b).*?([1-9]\d*)\s*x\s*([1-9]\d*)(?:\s= *x\s*([1-9]\d*))?');
>> returns: {a,1,1250,2500}>>
>> Shouldn't both results= be equal?

> The problem is, afair,= that there is some state in pg's regexp engine
> that= makes greedy/ungreedy decision once per regexp.

Yeah.  Without having traced through it, I'm fairly s= ure that in the
first case, we have "(a)" which has no gr= eediness, then ".*?" which
is non-greedy, and then that d= etermines the overall greediness as
non-greedy, so it goe= s for the shortest overall match not the longest.

In the second case, "(a|b)" is greedy because anything involvin= g "|"
is greedy, so we immediately decide we'll be greedy= overall.

The fine manual explains how= you can force greediness or non-greediness
when the engi= ne's default rules for that don't do what you want.

            rega= rds, tom lane

------=_Part_1746772_203074880.1675066286327--