View Ticket
Not logged in
Ticket UUID: 11250a236da29e6badf7755a1b67995defa5170
Title: unexpected non greedy matching behaviour
Type: Bug Version: 8.6
Submitter: anonymous Created on: 2015-05-17 14:55:33
Subsystem: 43. Regexp Assigned To: dkf
Priority: 5 Medium Severity: Minor
Status: Closed Last Modified: 2015-05-19 17:02:17
Resolution: Fixed Closed By: ferrieux
    Closed on: 2015-05-19 17:02:17
Description:
since there seems no suitable mailing list to ask and I presume it's a bug I'm reporting this here: non-greedy matching seems not to work as it should in certain contexts, specifically, matching seemingly becomes greedy if the respective atom is preceded by a pattern containing `+'  to indicate arbitrary (one or more) occurrence of chars from a range or class.

example:

# a string with four sub-patterns I want to capture (the latex-like constructs `\emph{xxx}'):

set s "\emph{A} a \emph{B} b \emph{C} c \ital{D} d"

# this one correctly finds all four of them, when specifying that the stuff before `{xxx}' consists of exactly 4 letters:

regexp -all  {(\\[a-z]{4})\{(.*?)\}} $s dum; puts $dum; # ==> \ital{D}  <== (OK)

# allowing "one or more" chars instead of "exactly 4" changes the match to "greedy", i.e. it reports a _single_ match of
# everyting up to the last closing `}':

regexp -all  {(\\[a-z]+)\{(.*?)\}} $s dum; puts $dum; # == > \emph{A} a \emph{B} b \emph{C} c \ital{D} <==(WRONG)

# the same problem here:
regexp -all  {(\\w+)\{(.*?)\}} $s dum; puts $dum; # == > \emph{A} a \emph{B} b \emph{C} c \ital{D} <==(WRONG)


is this a bug or am I completely misunderstanding how non-greedy matching is supposed to work/be used?

thanks 

joerg
User Comments: ferrieux added on 2015-05-19 17:02:17:
The recommended, active community meeting point is the comp.lang.tcl newsgroup.

aku added on 2015-05-19 16:50:27:
Top of the <a href="http://core.tcl.tk/">core</a> website lists a number of related websites and channels, like comp.lang.tcl.

anonymous added on 2015-05-19 10:57:55:
short feedback: I like the change to the manpage and believe it is helpful. in absence of another "communication channel" and  if someone from core is reading this: I am very new to tcl and am not able to find a suitable tcl-users mailing list. isn't there any? I would not have reported my regexp problem as a bug in the first place if I would have found a means to ask the community otherwise. so, what do you recommend? it seems wrong to flood the bug tracker with stuff like this inquiry ...

thx/joerg

dkf added on 2015-05-18 08:24:54:

Added text (post-render):

              NOTE:  This  means  that you can usually make a RE be non‐greedy
              overall by putting {1,1}? after one of the first  non‐constraint
              atoms or parenthesized sub‐expressions in it. It pays to experi‐
              ment with the placing of this non‐greediness override on a suit‐
              able  range  of input texts when you are writing a RE if you are
              using this level of complexity.

              For example, this regular expression  is  non‐greedy,  and  will
              match  the  shortest substring possible given that “abc” will be
              matched as early as possible (the  quantifier  does  not  change
              that):

                     ab{1,1}?c.*x.*cba

              The  atom  “a”  has no greediness preference, we explicitly give
              one for “b”, and the remaining quantifiers are overridden to  be
              non‐greedy by the preceding non‐greedy quantifier.


dkf added on 2015-05-18 08:22:27:

Done, but I don't think it helps as much as people really want; the real problem is that it is a long page with a lot of content on it. I don't know what to do about that without making the problem worse.


anonymous added on 2015-05-17 18:38:35:
I see now after looking long and hard at `man re_syntax' that it somehow _is_ explained but making this more obvious would be good. it seems a solution in my case is to to use

regexp -all  {\\(?:\w+){1,1}?{(.*?)}} $s dum

this "trick" of appending `{1,1}?' to the greedy part in order to make it non-greedy overall (if it _is_ the way to solve this, might especially deserve to be pointed out more pronouncedly.

ferrieux added on 2015-05-17 15:41:42:
As it turns out, Tcl regexps don't allow for mixed-greediness:

http://stackoverflow.com/questions/11771610/tcl-regular-expression-help-needed-on-non-greedy-quantifiers-in-pattern-with

I admit that this fact could be made more conspicuous in the re_syntax manpage though.

Donal, care for a doc fix ?