can't force regex subexpression greediness as stated by man page
Open524b35a8b5f9ec643fc323582217193d91247430Created 8 years agoUpdated 22 days ago
Descriptionplain2019-05-12 18:45:58
The re_syntax man page states
The quantifiers \fB{1,1}\fR and \fB{1,1}?\fR can be used to
force longest and shortest preference, respectively, on a
subexpression or a whole RE.
However, this doesn't actually work reliably, for example
% regexp -inline {^(l*)(.*)(f*)$} {llmmmfff}
llmmmfff ll mmmfff {}
% regexp -inline {^(l*){1,1}?(.*)(f*)$} {llmmmfff}
llmmmfff ll mmmfff {}
The first result is fine, but the second result should have been
llmmmfff {} llmmmfff {}
that is, the (l*) should have been forced to be non-greedy.
I tracked this down to an over-optimistic optimization in parseqatom():
it throws away a {1,1} quantifier without regard to whether it's trying to impose
a different greediness than the atom would have natively.
This seems to be sufficient to fix it:
@@ -1155,7 +1155,10 @@ parseqatom(struct vars *v,
/* rest of branch can be strung starting from atom->end */
s2 = atom->end;
}
- else if (m == 1 && n == 1)
+ else if (m == 1 && n == 1 &&
+ (qprefer == 0 ||
+ (atom->flags & (LONGER | SHORTER | MIXED)) == 0 ||
+ qprefer == (atom->flags & (LONGER | SHORTER | MIXED))))
{
/* no/vacuous quantifier: done */
EMPTYARC(s, atom->begin); /* empty prefix */
(That's from a draft Postgres patch, your formatting of the code is different)
BTW, I think that the weasel wording in the NOTE in re_syntax.n immediately
after the above-quoted bit is due to this bug.
Background at
https://www.postgresql.org/message-id/flat/20583.1557680147%40sss.pgh.pa.us
Referenced by3
0437bd3e6eTicket [fe4fca32d9] mixed-greediness regular expressions need better documentation status still Open with 4 other changes ticket2026-11-10 16:40:2998ef5206beFix [524b35a8b5]: a {1,1} quantifier was always dropped as vacuous, so {1,1}? could not make a greedy atom non-greedy (and {1,1} a non-greedy one greedy). Keep it when it asks for a different preference than the atom has. Patch by Tom Lane. New tests reg-25.4 to 25.9. check-in2026-10-10 18:17:280040d90962Proposed fix for bug [524b35a8b5]. No test-case yet. check-in2019-05-12 20:28:59
jan.nijtmansplain2019-05-12 20:31:56
Proposed fix committed in bug-524b35a8b5 branch. No test-case yet.
serhiy.storchakawiki2026-10-10 18:18:30
Still the same in 8.6, 9.0 and 9.1: parseqatom() dropped every {1,1} as vacuous, so {1,1}? could not make a greedy atom non-greedy, and {1,1} could not make a non-greedy atom greedy (e.g. (.*?){1,1}(.*)), contrary to re_syntax.n.
Proposed fix in branch regexp-1-1-quantifier-preference: your patch, which keeps the quantifier when it asks for a different preference than the atom has. With it, the results agree with PostgreSQL on the example and on other {1,1} and {1} cases. Tests reg-25.4 to 25.9. Branch bug-524b35a8b5 had the same change without tests, based on core-8-5-branch.
oehharwiki2026-11-10 16:40:59
Jan, is there any additional action? Serhiy proposes to also merge the doc fix in [fe4fca32d].
Thanks for all, Harald
Log in (anonymous is fine) to add a comment.