Regular expression problem with groups
__peter__ at web.de
Mon Aug 16 17:13:37 CEST 2004
Axel Kowald wrote:
> Hi everybody,
> I have a 'simple' problem with regular expressions. Maybe someone can
> help me.
> import re
> bla = 'the the'
> obj = re.search('(.+) \1',bla)
> obj.group(1) should now be 'the'. Instead obj is none (python 2.3
> under windows and 2.1 under linux). The problem seems to be the \1
> where I try to reference the first group. However, every tutorial says
> this is the correct way to do it !?
> Any ideas what I'm doing wrong ?
You did neither escape the backslash nor use a raw string.
>>> re.search("(.+) \\1", "the the").group(1)
>>> re.search(r"(.+) \1", "the the").group(1)
The latter is the most convenient way to use with regular expressions with
the limitation that it may not end with an odd number of backslashes.
More information about the Python-list