Re: Regex y grouping

Chema Cortes <[email protected]>
Newsgroups gmane.comp.python.general.castellano
Message-ID <[email protected]>
El 28 de julio de 2009 17:40, Ferran Fontcuberta<[email protected]> escribió:

> Me gustaría poder utilizar algo tal que así:
>
> def func(p):
>        print p
>        return p
>
> re.sub('test(\d)', func(r'\1'), 'test0')
>
> Pero veo que no es posible; la conversión de \1 a aquello a que hace
> match parece ser que sólo funciona dentro de la expresión regular.
>
> ¿Alguna idea de cómo hacerlo?

Deberías repasar la documentación. Si necesitas pasar una función,
entoces recibes un objeto "matchobj", quien te dará la información que
necesitas:

def func(obj):
       s=obj.group(0)
       print s
       return s

re.sub('test(\d)', func, 'test0')
_______________________________________________
Lista de correo Python-es 
http://listas.aditel.org/listinfo/python-es
FAQ: http://listas.aditel.org/faqpyes
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.