faqts : Computers : Programming : Languages : Python : Common Problems : Regular Expressions

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

5 of 8 people (63%) answered Yes
Recently 3 of 6 people (50%) answered Yes

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