Regular expression problem with groups

Peter Otten __peter__ at
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 ='(.+) \1',bla)
> 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.

>>>"(.+) \\1", "the the").group(1)

>>>"(.+) \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 mailing list