[Tutor] Letters & Digits [regular expressions]
Danny Yoo
dyoo@hkn.eecs.berkeley.edu
Fri, 16 Nov 2001 14:42:56 -0800 (PST)
On Fri, 16 Nov 2001, Lee-Shanok, Bruce wrote:
> I know there are constants in Python that define all the digits,
> letters, printable characters, etc. What I'm wondering is if there's a
> clean and easy way to test each character in a string to see if they
> match (or do not match) characters from those lists. So far the best
> I've been able to come up with are sequences of for loops, but I'm
> imagining there must be a better way.
Hmmm... I know I should try to resist the urge to mention regular
expressions, but it's too hard! *grin*
Regular expressions may help you in this case --- they're used to find
specific patterns in text. Here's one example that may help you:
###
>>> only_digits = re.compile(r""" ^ # at the very start...
... \d+ # we only want digits!
... $ # Even up to the end.
... """, re.VERBOSE)
>>> only_digits.search("42")
<SRE_Match object at 0x8131c70>
>>> only_digits.search("hello")
>>> only_digits.search("he42llo")
>>> only_digits.search("1234567890")
<SRE_Match object at 0x81311e0>
###
The "only_digits" regular expression knows how to recognize a digit
string. You should be able to modify this example to make it a little
smarter.
You can find out more about regular expressions in the regex HOWTO on
Python.org:
http://py-howto.sourceforge.net/regex/regex.html
Also, there's a great regular expression tutorial from the Perl folks at:
http://www.perl.com/CPAN-local/doc/FMTEYEWTK/regexps.html
It's a fairly fun piece to read, and the concepts in the tutorial there
transfers nicely to Python.
Regular expressions are very powerful, and that's the problem: sometimes,
they're just so seductive that they obscure potentially better solutions.
Hope this helps!