Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

issue with non-greedy operator ".*?"
Am I wrong here or is this a bug in TCL ?

The non-greedy operator ".*?" does not work as expected when used with
\d+ or \S+ option preceding it.

expect1.8> info tclversion
8.4
expect1.9> set str {1 2 3}
1 2 3
expect1.10> regexp {\d+.*?\d+} $str match
1
expect1.11> puts $match
1 2 3

----->>> regexp should match {1 2} in the above statement since the
non-greedy operator ".*?" is used.

expect1.12> regexp {\d.*?\d} $str match
1
expect1.13> puts $match
1 2

----->>>> Works fine when using "\d" instead of "\d+" in the above
statement

Expert advice needed ...

Regards
Sharad

Report this thread to moderator Post Follow-up to this message
Old Post
Sharad
04-01-08 01:21 PM


Re: issue with non-greedy operator ".*?"
Sharad wrote:
> Am I wrong here or is this a bug in TCL ?
>
> The non-greedy operator ".*?" does not work as expected when used with
> \d+ or \S+ option preceding it.

Documented behavior. Mixing greedy and non-greedy quantifiers is
tricky to say at best, reread:
http://www.tcl.tk/man/tcl8.5/TclCmd/re_syntax.htm#M95 (Matching, first
three paragraphs dealing with preference).

Using \d+ before .*? in your first example switches the preference to
'greedy', while using \d (no preference) then .*? (non-greedy) sets
preference to non-greedy for the whole expression.

Michael



Report this thread to moderator Post Follow-up to this message
Old Post
schlenk
04-01-08 01:21 PM


Re: issue with non-greedy operator ".*?"
Thanks Michael !!!

On Apr 1, 5:45 pm, schlenk <schl...@uni-oldenburg.de> wrote:
> Sharad wrote: 
> 
>
> Documented behavior. Mixing greedy and non-greedy quantifiers is
> tricky to say at best, reread:http://www.tcl.tk/man/tcl8.5/TclCmd...htm#
M95(Matching, first
> three paragraphs dealing with preference).
>
> Using \d+ before .*? in your first example switches the preference to
> 'greedy', while using \d (no preference) then .*? (non-greedy) sets
> preference to non-greedy for the whole expression.
>
> Michael


Report this thread to moderator Post Follow-up to this message
Old Post
Sharad
04-01-08 01:21 PM


Re: issue with non-greedy operator ".*?"
Sharad wrote:
> The non-greedy operator ".*?" does not work as expected when used with
> \d+ or \S+ option preceding it.
>   expect1.10> regexp -inline {\d+.*?\d+} "1 2 3"
>   {1 2 3}
> Expert advice needed ...

That's the way our RE engine is documented to work, and is a significant
difference from the Perl-derived RE engines. The complication has to do
with the difference between recursive engines and automata-based engines
(the former handle this case "better", but the latter are better at
other types of match). Alas, the conflict can't be resolved easily;
there is a deep theoretic trade-off between the two (CS is good for some
things at least!) so the best advice is "don't mix greediness in a
single RE". It's not that you can't, but it's a recipe for confusion.

Better to use a different RE:
% regexp -inline {\d+\D*\d+} "1 2 3"
{1 2}

Donal.

Report this thread to moderator Post Follow-up to this message
Old Post
Donal K. Fellows
04-02-08 09:52 AM


Re: issue with non-greedy operator ".*?"
Donal ... I agree. Its intentionally calling for trouble. To make sure
that the code doesn't break (which is important), its better to avoid
such tricky things.

I just wanted to clarify my doubts ... thanks to all those who shared
their views. Appreciate your help.


Report this thread to moderator Post Follow-up to this message
Old Post
Sharad
04-03-08 01:47 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Tcl archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 06:24 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.