Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

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*

Jemts

"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."

Comment on Question on REGEX
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*/'

        HTH!

        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 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

    -moen-

      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 ;)

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://80108]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2014-09-19 04:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (129 votes), past polls