Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Dominus on the non-greedy version of the ? quantifier

by Aristotle (Chancellor)
on Aug 06, 2006 at 21:10 UTC ( [id://565864]=perlquestion: print w/replies, xml ) Need Help??

Aristotle has asked for the wisdom of the Perl Monks concerning the following question:

So I was just looking at DominusRegular Expression Mastery talk, since I hadn’t seen it before. I’ve been around the block enough times to know the subject matter cold, but of course understanding and mastery are separate issues and it’s always good to take another look – in complex subjects, other people will often bring up angles to look at some of their aspects that you never thought of before. Anyway, I was skimming the slides, but it didn’t look like I’d be taking home anything from this talk.

Until I came to the absolute end, the very last bullet of the very last slide:

  • Also there’s a non-greedy version X??
  • I used to pay US$60 for a live sighting of ?? in the wild
  • But one day I thought of
    if ( $option =~ /^-f(i(e(ld??)??)??)??$/ ) { ... }

I’ve been staring at that example for 10 minutes and I really can’t see how it would ever match differently from the greedy version:

/^-f(i(e(ld?)?)?)?$/

From what I can tell, since the entire pattern is anchored at both ends and the captures are nested, it must always match the exact same things, whether the quantifiers are greedy or not.

Am I missing something?

Makeshifts last the longest.

Replies are listed 'Best First'.
Re: Dominus on the non-greedy version of the ? quantifier
by rinceWind (Monsignor) on Aug 06, 2006 at 22:36 UTC

    My take on regexp greediness, is that this affects the order in which matches are tried.

    The greedy form of * and + try the longest match first, and successively decreasing the candidate match. Non-greedy forms *? and +? start with the shortest match and gradually increase the length.

    The ? operator matches 0 or 1 occurrence. 1 occurrence is tried first. The ?? operator tries 0 occurrences first.

    In the example you have given, it does not matter which is tried first, as you will get the same result. If there were another quantifier downstream of the ??, this could make a difference, as it would see the string to the left of the clause matching the ?? first, wherease a greedy ? would present the remainder of the string to the right of the match, first.

    --

    Oh Lord, won’t you burn me a Knoppix CD ?
    My friends all rate Windows, I must disagree.
    Your powers of persuasion will set them all free,
    So oh Lord, won’t you burn me a Knoppix CD ?
    (Missquoting Janis Joplin)

      I know what the quantifiers mean and how greediness works – which is exactly why I’m asking. I cannot see what Dominus is hinting at by bringing up this particular example, because greediness does not affect its result in any way.

      I was hoping someone would have an idea as to what he might have been thinking.

      Makeshifts last the longest.

        ...because greediness does not affect its result in any way.

        I was hoping someone would have an idea as to what he might have been thinking.

        You're only thinking about the final outcome. Sometimes, changes are not made for the outcome, but because it changes how it performs, and one method may be better for the normally expected input.

        Unfortunately, in this case, I'm not seeing that on my system the non-greedy being significantly better at matching the short version, and it's significantly worse at matching the long version:

        Oh -- and it _does_ change the values of $1, $2, $3, but we have no reason from the code snippet given that they're important.,/strike>

        Update: Removed extra comma, resulting in sloppy english grammer, and stressed the 'not'. Was :

        Unfortunately, in this case, I'm not seeing that on my system, the non-greedy being significantly better at matching the short version ...

        Update: tye is correct in the lack of difference. Why a non-greedy expression would match 1 when it has the option of matching 0 makes no sense to me, though.

Re: Dominus on the non-greedy version of the ? quantifier
by tilly (Archbishop) on Aug 09, 2006 at 00:57 UTC
    Yes, you're missing something.

    The point is not what the code would do when run. It is what it could do to Dominus' wallet.

      Oh, d’oh. Thanks for the pointer. :-)

      Makeshifts last the longest.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://565864]
Approved by GrandFather
Front-paged by astaines
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (10)
As of 2024-04-18 12:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found