On Sun, Jul 6, 2014 at 4:51 AM, <rxjw...@gmail.com> wrote:
> I just begin to learn Python. I do not see the usefulness of '*' in its
> description below:
> The first metacharacter for repeating things that we'll look at is *. *
> match the literal character *; instead, it specifies that the previous
> can be matched zero or more times, instead of exactly once.
> For example, ca*t will match ct (0 a characters), cat (1 a), caaat (3 a
> characters), and so forth.
> It has to be used with other search constraints?
(BTW, this is a regexp question, not really a Python question per se.)
That's usually when it's useful, yeah. For example, [0-9] matches any
of the characters 0 through 9. So to match a natural number written in
decimal form, we might use the regexp [0-9][0-9]*, which matches the
strings "1", "12", and "007", but not "" or "Jeffrey".
Another useful one is `.*` -- `.` matches exactly one character, no
matter what that character is. So, `.*` matches any string at all.
The power of regexps stems from the ability to mix and match all of
the regexp pieces in pretty much any way you want.