[Python-es] Perl style regex's en Python
Ferran Fontcuberta
ferran en fompi.net
Lun Mar 29 13:14:52 CEST 2010
En la linea "útiles inútiles", me acabo de encontrar con esto; por si a
alguien le interesa:
Source: http://code.activestate.com/recipes/384122-infix-operators/
Modulo: http://pypi.python.org/pypi/inrex/1.1.1
easy_install inrex
from inrex import match, search, split, findall, finditer
if 'asd 123' |match| r'(\w+) (\d+)':
print 'word is', match[0]
print 'digit is', match[1]
if 'asd 123' |match| r'(?P<word>\w+) (?P<digit>\d+)':
print 'word is', match['word']
print 'digit is', match['digit']
print 'asd 123 qwe 456' |findall| r'\d+'
print 'asd 123 qwe 456' |split| r'\d+'
print 'asd 123 qwe 456' |split(maxsplit=1)| r'\d+'
with open("logfile.txt") as f:
for m in f |match| r"(\w+) (\w+) (\w+)":
print match[0], match[1], match[2]
# or
print m.group(0), m.group(1), m.group(2)
--
Ferran at fompi.net
another sexy techie
Más información sobre la lista de distribución Python-es