Artifacts Associated With Ticket 2798939fffffffff
Ticket change [9955c485fa] (rid 110775) by bilgexa on 2009-05-30 21:15:37:
- assignee initialized to: "pvgoran"
- cmimetype initialized to: "text/plain"
- comment initialized to:
Non-greedy quantifiers revert to greedy when used in conjunction with branches (pipe character). The following branch-free expression matches each letter in the test string and therefore correctly returns 14 regexp -all .+? {this is a test} The following branched expression matches the entire string only once because the quantifier reverts to greedy and therefore incorrectly returns 1 regexp -all .+?|x {this is a test} This breaks a lot of complex regular expressions that require non-greedy quantifiers and branches. - foundin initialized to: "obsolete: 8.5.7"
- is_private initialized to: "0"
- priority initialized to: "5 Medium"
- resolution initialized to: "None"
- status initialized to: "Open"
- submitter initialized to: "bilgexa"
- subsystem initialized to: "43. Regexp"
- title initialized to:
Non-greedy quantifiers revert to greedy when branched
- type initialized to: "Bug"
Ticket change [9d0f61643b] (rid 96247) by dkf on 2009-05-31 16:55:25:
- icomment:
IIRC, the problem is that the "first" quantifier in the syntax tree is the alternation, which can't be made non-greedy. The fix is to use {1,1}? to force the whole RE to be non-greedy (a non-capturing paren is also needed, but that's pretty trivial). % regexp -all (?:.+?|x){1,1}? {this is a test} 14 Note that this is different from Perl/PCRE; they use a different model for greedy/non-greedy handling that does this in a way that is more as people intuitively expect (in return for being worse in other cases). - login: "dkf"
- mimetype: "text/plain"
- username: "dkf"
- icomment:
Ticket change [4b312960d1] (rid 126656) by bilgexa on 2009-05-31 18:47:22:
- icomment:
Surely you realise that this "fix" cannot possibly apply to complex alternations. The non-greedy quantifiers within the alternation group are all broken and this functionality can never be replaced by a single quantifier on the group as a whole.
- login: "bilgexa"
- mimetype: "text/plain"
- username: "bilgexa"
- icomment:
Ticket change [f74c071c3e] (rid 157325) by dram on 2017-10-26 01:36:45:
- assignee changed to: "aku"
- closer changed to: "nobody"
- comment changed to:
Non-greedy quantifiers revert to greedy when used in conjunction with branches (pipe character). The following branch-free expression matches each letter in the test string and therefore correctly returns 14 regexp -all .+? {this is a test} The following branched expression matches the entire string only once because the quantifier reverts to greedy and therefore incorrectly returns 1 regexp -all .+?|x {this is a test} This breaks a lot of complex regular expressions that require non-greedy quantifiers and branches. - icomment:
Still true for 8.6.6 But this behaviour is stated clearly in re_syntax(n) as: An RE consisting of two or more branches connected by the | operator prefers longest match. BTW, PostgreSQL has same behaviour. (confirmed in PG 9.6.4)
- login: "dram"
- mimetype: "text/plain"
- severity changed to: "Minor"
Ticket change [56d4066d01] (rid 157331) by dgp on 2017-10-26 02:10:01:
- icomment:
Tcl and postgres make use of (forks of) the same regexp engine.
- login: "dgp"
- mimetype: "text/plain"
- icomment:
Ticket change [65a2f7a6b9] (rid 257600) by serhiy.storchaka on 2026-10-11 10:49:37:
- icomment:
Documented: an RE with a top-level <code>|</code> prefers the longest match. <code>(?:.+?|x){1,1}?</code> makes it prefer the shortest; <code>regexp -all</code> then returns 14. The docs are clarified in branch [/timeline?r=doc-re-syntax-preference|doc-re-syntax-preference]. Duplicate of [219219ffff]. - login: "serhiy.storchaka"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [8ec6abd2c0] (rid 257606) by serhiy.storchaka on 2026-10-11 10:52:40:
- login: "serhiy.storchaka"
- resolution changed to: "Duplicate"
- status changed to: "Closed"
Ticket change [9c36b362b5] (rid 259884) by serhiy.storchaka on 2026-10-22 17:19:24:
- closedate changed to: "2461324.9532507984"
- closer changed to: "serhiy.storchaka"
- login: "serhiy.storchaka"