Welcome to the Monastery PerlMonks

### Question on REGEX

by Jemts (Monk)
 on May 14, 2001 at 01:37 UTC 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."

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*/'

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

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

Create A New User
Node Status?
node history
Node Type: perlquestion [id://80108]
Approved by root
help
Chatterbox?
 [Eily]: I just wrote a oneliner to solve a (non perl) colleague's problem, and it worked on the first try [Eily]: and he wasn't impressed 1nickt adds gingersnaps to the platter on the sideboard. [Eily]: sure it was a pretty standard one liner (of the \$h{\$1} = \$2 if /regex/; END { pp \%h; } variety), but he's not supposed to know that! [Eily]: what's the point of writing perl if people aren't impressed :P

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (10)
As of 2017-04-26 15:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
I'm a fool:

Results (482 votes). Check out past polls.