Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Question on REGEX

by Jemts (Monk)
on May 14, 2001 at 01:37 UTC ( #80108=perlquestion: print w/replies, xml ) Need Help??
Jemts has asked for the wisdom of the Perl Monks concerning the following question:

Is there a difference between
a+? and a*


"If you're traveling in a time machine, and you're eating corn on the cob, I don't think it's going to affect things one way or the other. But here's the point I'm trying to make: Corn on the cob is good, isn't it."

Replies are listed 'Best First'.
Re: Question on REGEX
by arturo (Vicar) on May 14, 2001 at 01:51 UTC

    A large one: +? says "match the shortest set of one or more of these", (so, a+? matches "a" in "aaaaaaaa"), while * says "match 0 or more of these"; a* matches "bob".

    See Quantifiers in Regular Expressions in the Tutorials section of this site, or read perldoc perlre on your system.

    More generally, see How to RTFM for information about where things like this are documented on your system.

    perl -e 'print "How sweet does a rose smell? "; chomp $n = <STDIN>; $r +ose = "smells sweet to degree $n"; *other_name = *rose; print "$other +_name\n"'

      I would be pretty sure that a* doesn't match "bob". I think you mean, a* will match the zero-width character at the start of "bob"?

        Pay attention to the exact wording. * is a quantifier, it says "match ZERO OR MORE". There's no particular character it matches, it's just that any string you care to pass it satisfies the condition "has zero or more 'a's in it".

        By itself, /a*/ isn't a very useful regular expression (nor, for that matter, is /a+?/ as opposed to simply /a/, because that will match a single 'a' and no more (the shortest string of one or more "a"s is one "a").

        Try it :perl -e 'print "Yee haw!\n" if "bob" =~ /a*/'

        Better yet, perl -e 'print "Yee haw!\n" if undef =~ /a*/'


        perl -e 'print "How sweet does a rose smell? "; chomp $n = <STDIN>; $r +ose = "smells sweet to degree $n"; *other_name = *rose; print "$other +_name\n"'
Re: Question on REGEX
by japhy (Canon) on May 14, 2001 at 03:00 UTC
    Perhaps you were thinking of (a+)? vs. a*, which are technically the same. But a+? means "one or more of 'a', but match it as few times as possible", whereas a* means "zero or more of 'a', but match it as many times as possible".

    There will be an exercise in LPRE which is to convert quantifiers back and forth.

    japhy -- Perl and Regex Hacker
Re: Question on REGEX
by moen (Hermit) on May 14, 2001 at 01:59 UTC
    a+? matches the minimal amount of "a" as long as there are 1 or more "a"'s (non greedy) while
    a* matches "a" and maximum amount of anything as long as there are 0 or more (greedy)

    -note: did a minor update to improve poor wording


      this is incorrect, or at least misleading.
Re: Question on REGEX
by bobione (Pilgrim) on May 14, 2001 at 02:00 UTC
    Yes ! There More Than One Way To Do It, but Everything Is Different.
    a* -> means zero character 'a' or more.
    a+? -> means one character 'a' or more and the '?' is for none greedy (the smaller match of your regexp). That not the same as (a+)? which is optional here (because of '?').
    I hope I am clear enough and that enlight you.

    BoBiOne KenoBi ;)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://80108]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2017-06-25 00:50 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (563 votes). Check out past polls.