\number parameter in regular expression

Piet pit.grinja at gmx.de
Sat Sep 25 09:19:50 EDT 2004


Hi there,
I am trying to understand the correct use of the \number parameter in
pythons regular expressions. I belive that the following should work
import re
pattern = re.compile("([a-zA-Z]*)(.*)(\d{4}-\d{2}-\d{2})\1")
search = pattern.search("Date#ThisIsASpacer2004-09-25Date")
print search.groups()
But it doesn´t, because the RE doesn´t match.
I thought that the "\1" thing indicates that the respective part of
the string in question should match the one that is located in
search.group(1). When I change the script to
import re
pattern = re.compile("([a-zA-Z]*)(.*)(\d{4}-\d{2}-\d{2})")
search = pattern.search("Date#ThisIsASpacer2004-09-25")
print search.groups()
print search.group(1)
I get the expected result:
("Date","#ThisIsASpacer","2004-09-25")
Date
How is the \number parameter used correctly?
Many thanks in advance
Piet



More information about the Python-list mailing list