\number parameter in regular expression

Michael Hoffman m.h.3.9.1.without.dots.at.cam.ac.uk at example.com
Sat Sep 25 09:31:52 EDT 2004


Piet wrote:

> import re
> pattern = re.compile("([a-zA-Z]*)(.*)(\d{4}-\d{2}-\d{2})\1")

You should be using a raw string, r"([a-z...". As a purely stylistic 
concern, I found that calling this regex object "pattern" somewhat 
confusing since that is normally what one calls the string that is 
compiled into a regex.

regex = re.compile(r"([a-zA-Z]*)(.*)(\d{4}-\d{2}-\d{2})\1")
                     ^

 >>> regex.search("Date#ThisIsASpacer2004-09-25Date").groups()
('Date', '#ThisIsASpacer', '2004-09-25')
--
Michael Hoffman



More information about the Python-list mailing list