Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 98310803f7c4861ecb1eff727efb0c2259a60766bc19a8f24dca8dd9d097d96e
Ticket: 524b35a8b5f9ec643fc323582217193d91247430
can't force regex subexpression greediness as stated by man page
User & Date: tgl on 2019-05-12 18:45:58
Changes

  1. assignee changed to: "nobody"
  2. closer changed to: "nobody"
  3. cmimetype changed to: "text/plain"
  4. comment changed to:
    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
    
  5. foundin changed to: "8.6.8"
  6. is_private changed to: "0"
  7. login: "tgl"
  8. priority changed to: "5 Medium"
  9. private_contact changed to: "26864b65f1c6ce8bda86f87ed9c554e19ac0390d"
  10. resolution changed to: "None"
  11. severity changed to: "Minor"
  12. status changed to: "Open"
  13. submitter changed to: "tgl"
  14. subsystem changed to: "43. Regexp"
  15. title changed to:
    can't force regex subexpression greediness as stated by man page
    
  16. type changed to: "Bug"