in reply to Question on REGEX

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

Replies are listed 'Best First'.
Re: Re: Question on REGEX
by kal (Hermit) on May 14, 2001 at 16:50 UTC

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

        We're obviously differing in semantics, then :) I would say a* doesn't match bob because trying /(a*)/ wouldn't get you "bob".

        In the examples you give, the regexer is satisfied, but I wouldn't say the regex a* matches bob. In my parlanace, I would say (for example) a(b|a) would match ab. Then, a* doesn't match bob because a(a|b)a*a(a|b) won't match abbobab.

        Does anyone else share my terminology? Perhaps this is a function of the books we read...