Entry
Could someone give me a simple explanation of regular expressions?
Jun 20th, 2000 05:46
unknown unknown, Andrew Kuchling
Regular expressions are a mini-language for defining a certain set
of strings; the re module then takes a regular expression and tells you
if a given string matches the expression. For example, [abc] means
'a', 'b', or 'c'; \bA.*?\b means a word beginning with 'A' (\b is a
special sequence meaning "match a word boundary"). As you can see,
regular expressions are concise but can easily be obscure.
Regular expressions will practically never be faster than a simple
string.find, so if you're searching for a fixed string, don't use
regular expressions; their extra expressive power costs you in
performance. But they can do things that aren't possible with the
string module alone, and would require several lines of Python code
to implement.
Consult the Library Reference, or the Regular Expression HOWTO for
more info. (http://www.python.org/doc/current/lib/module-re.html,
http://www.python.org/doc/howto/regex/)