Artifacts Associated With Ticket 3574242fff
Ticket change [8c423c8b7e] (rid 127718) by sebres on 2012-10-03 22:30:52:
- assignee initialized to: "sebres"
- cmimetype initialized to: "text/plain"
- comment initialized to:
Possible bug, cause IMO that should match the same: Or a documentation bug - according to documentation "{m}?" is ungreedy quantifier. % regexp -inline {((?:ab|abc){1}?)(cd|d){1}?} abcd abcd abc d % regexp -inline {((?:ab|abc){1,1}?)(cd|d){1,1}?} abcd abcd ab cd - foundin initialized to: "None"
- is_private initialized to: "0"
- priority initialized to: "7 High"
- resolution initialized to: "None"
- status initialized to: "Open"
- submitter initialized to: "sebres"
- subsystem initialized to: "43. Regexp"
- title initialized to:
exact match is ungreedy by reluctant exact match quantifier
- type initialized to: "Bug"
Ticket change [b24d6d0b82] (rid 98310) by sebres on 2012-10-04 05:31:23:
- title changed to:
exact match is greedy by reluctant exact match quantifier
- title changed to:
Ticket change [5bef2df79f] (rid 177249) by sebres on 2019-11-22 14:52:40:
- closer changed to: "nobody"
- comment changed to:
Possible bug, cause IMO that should match the same: Or a documentation bug - according to documentation "{m}?" is ungreedy quantifier. % regexp -inline {((?:ab|abc){1}?)(cd|d){1}?} abcd abcd abc d % regexp -inline {((?:ab|abc){1,1}?)(cd|d){1,1}?} abcd abcd ab cd - icomment:
A fresh concern regarding the issue and new findings after discussion in tclchat with pooryorick. The issue could be explained with following sentence from the documentation:<br/> <blockquote><i>A quantified atom with quantifier {m} or {m}? has the same preference (possibly none) as the atom itself. A quantified atom with other normal quantifiers (including {m,n} with m equal to n) prefers longest match. A quantified atom with other non-greedy quantifiers (including {m,n}? with m equal to n) prefers shortest match.</i></blockquote> But the fact that <code>{m,n}</code> prefers the shortest match doesn't mean that the sub-expression it is applied to should prefer the shortest match (at least, it doesn't necessarily mean that). <hr> Another interesting set of examples which can be related to the same issue: <pre> % regexp -inline {(t*?)?} ttt ttt <b style="color:green">t</b><b style="color:red">tt</b> % regexp -inline {(t*)?} ttt ttt ttt </pre> Although it behaves like described in the documentation:<br/> <blockquote><i>A branch has the same preference as the first quantified atom in it which has a preference. An RE consisting of two or more branches connected by the | operator prefers longest match.</i></blockquote> But it does not explain why the group is expanded to the whole match, as well as the difference to this counter-example: <pre> % regexp -all -inline {(t)*} ttt ttt <b style="color:green">t</b> </pre> So the change to greedy quantifier of RE seems to affect capturing behavior of (initially) non-greedy group, compared to (initially) greedy group. <hr> FWIW, maybe following would explain the reason (or rather the necessity) why or for which cases the sub-branch (group) could be non-greedy (why this can be expected or used): <pre> % regexp -inline {(t*?(\w+))?} ttxx ttxx ttxx ttxx % regexp -inline {(t*(\w+))?} ttxx ttxx ttxx xx </pre> But that all still don't explain the growth of captured non-greedy group to the whole match. - login: "sebres"
- mimetype: "text/x-fossil-wiki"
- severity changed to: "Minor"
Ticket change [80b00c7c7b] (rid 177265) by sebres on 2019-11-22 17:13:34:
- icomment:
If the issue is "caused" because RE-engine following main rule "greediness of topmost expression wins" or like a Kevin's statement: <blockquote><i>A general rule is that the greediness of the outermost quantifier determines the greediness of everything in the subexpression.</i></blockquote> it could explain why initially non-greedy subexpression grows, even to fulfill the whole match be greedy. So for example here the first capturing group grows to whole match, the second (inner) group capturing <b style="color:blue"><code>aaaf</code></b> and <b style="color:#A09"><code>aaf</code></b> each after other (so ultimately gets the value of the last one). <pre> % regexp -inline {((a*f)*?)<b>?</b>} <b style="color:blue">aaaf</b><b style="color:#A09">aaf</b> <b style="color:blue">aaaf</b><b style="color:#A09">aaf</b> <b style="color:blue">aaaf</b><b style="color:#A09">aaf</b> <b style="color:#A09">aaf</b> </pre> But there is just another example illustrating that this "concept" is not completely correct: <pre> % regexp -inline {((a*f)*?)<b style="color:green">*</b>} <b style="color:blue">aaaf</b><b style="color:#A09">aaf</b> <b style="color:blue">aaaf</b><b style="color:#A09">aaf</b> <b style="color:#A09">aaf</b> <b style="color:#A09">aaf</b> % regexp -inline {((a*f)*?)<b style="color:red">+</b>} <b style="color:blue">aaaf</b><b style="color:#A09">aaf</b> <b style="color:blue">aaaf</b><b style="color:#A09">aaf</b> {} {} </pre> Both of topmost quantifiers (red/green) are greedy, the whole expression is greedy.<br/> The whole result corresponds greedy whole match (matches the whole string in both cases).<br/> But in the second case the groups are empty (so it is somehow non-greedy anymore).<br/> So the (more aggressive) quantifier <code>+</code> behaves <b>fewer</b> greedy as (fewer aggressive) quantifier <code>*</code> inside the capturing groups. - login: "sebres"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [4c12dfb3b7] (rid 177269) by sebres on 2019-11-22 20:24:39:
- icomment:
After pretty productive discussion in tclchat with Andrew Gierth (RhodiumToad) we have new other findings now. The new handling of quantifier "+" (in contrary to "*") as well as "{1,}" (in contrary to "{0,}") makes impossible to retrieve indices of last successful matched capturing groups, which results to impossibly to differentiate which sub-group (expression) causes the match exactly. I hope following example would illustrate that: <pre> % regexp -indices -inline <b style="background-color:#dfd">{<b style="background-color:#dff">(<b style="color:red">(a?)*</b>|<b style="color:blue">(b?)*</b>)</b>*}</b> aaa <b style="background-color:#dfd">{0 2} <b style="background-color:#dff">{0 2} <b style="color:red">{2 2}</b></b></b> <b style="color:blue">{-1 -1}</b> % regexp -indices -inline <b style="background-color:#dfd">{<b style="background-color:#dff">(<b style="color:red">(a?)*</b>|<b style="color:blue">(b?)*</b>)</b>+}</b> aaa <b style="background-color:#dfd">{0 2}</b> <b style="background-color:#dff">{3 2}</b> <b style="color:red">{-1 -1}</b> <b style="color:blue">{-1 -1}</b> </pre> So it is basically impossible to differentiate which of last child sub-group <b style="color:red">(a?)*</b> or <b style="color:blue">(b?)*</b> caused a match of the parent (which is also mistakenly an empty match outside of string - <b style="background-color:#dff"><code>{3 2}</code></b>), as well as the to retrieve the last indices for that, because both are <b><code>{-1 -1}</code></b>, if "+" quantifier was used in some parent expression. The same is valid for this (note the "*" instead of "?" in both last nesting groups): <pre> % regexp -indices -inline <b style="background-color:#dfd">{<b style="background-color:#dff">(<b style="color:red">(a*)*</b>|<b style="color:blue">(b*)*</b>)</b>*}</b> aaa <b style="background-color:#dfd">{0 2} <b style="background-color:#dff">{0 2} <b style="color:red">{0 2}</b></b></b> <b style="color:blue">{-1 -1}</b> % regexp -indices -inline <b style="background-color:#dfd">{<b style="background-color:#dff">(<b style="color:red">(a*)*</b>|<b style="color:blue">(b*)*</b>)</b>+}</b> aaa <b style="background-color:#dfd">{0 2}</b> <b style="background-color:#dff">{3 2}</b> <b style="color:red">{-1 -1}</b> <b style="color:blue">{-1 -1}</b> </pre> Either one would expect the same result as using "*" instead of "+" here.<br/> Or if the decision is sub-expression matches empty string, the indices shall be still inside the parent (empty but not outside), so we should get something like this for both cases with "+" quantifier:<br/> <pre> % regexp -indices -inline <b style="background-color:#dfd">{<b style="background-color:#dff">(<b style="color:red">(a?)*</b>|<b style="color:blue">(b?)*</b>)</b>+}</b> aaa % regexp -indices -inline <b style="background-color:#dfd">{<b style="background-color:#dff">(<b style="color:red">(a*)*</b>|<b style="color:blue">(b*)*</b>)</b>+}</b> aaa <b style="background-color:#dfd">{0 2} <b style="background-color:#dff">{2 1} <b style="color:red">{2 1}</b></b></b> <b style="color:blue">{-1 -1}</b> </pre> Andrew is also going to file this as a Postgres bug...<br/> Here is [https://www.postgresql.org/message-id/16133-a8934caee4e53035%40postgresql.org|the issue in PG]. - login: "sebres"
- mimetype: "text/x-fossil-wiki"
- icomment: