Regular expression problem with groups

Gerardo Herzig -Departamento de Proyectos Especiales e Internet- Facultad de Medicina gherzig at fmed.uba.ar
Tue Aug 17 16:11:38 EDT 2004


El Lun 16 Ago 2004 12:08, Axel Kowald escribió:

When use .+, the regex engine try to take the longest posible match, so now .+ 
matches the entire `bla` variable. You need some  sort of separator (space 
perhaps) and then you can group:

you can try
re.search('([^ ]*) \\1', bla) 
instead. Watch the [^ ]* meaning "anything but a single space", then the space 
(as a separator) and last a escaped \1.

Works on python 2.2 under Linux.
Gerardo
> 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 ?
>
> Many thanks,
>
>                Axel

-- 
Gerardo Herzig
Departamento de Proyectos Especiales e Internet
Facultad de Medicina
U.B.A.



More information about the Python-list mailing list