reg exp help
JRCondon
jcondon at air-worldwide.com
Tue Jul 27 16:33:29 EDT 2004
No need for regex here; you can do this with strings:
>>> text = '5EHello'
>>> def lengthCheckedString(text):
from string import split
stext = split(text,'E',1)
if int(stext[0]) == len(stext[1]):
return stext[1]
else:
return None
>>> lengthCheckedString(text)
'Hello'
But, if you want to use a regex:
>>> def lengthCheckedStringRE(text):
import re
m = re.match('(?P<txtlen>\d+)E(?P<txt>.*$)',text)
txt = m.group('txt')
if int(m.group('txtlen')) == len(txt):
return txt
else:
return None
>>> lengthCheckedStringRE(text)
'Hello'
JRC
More information about the Python-list
mailing list