Newbie question about Re
Mike Fletcher
mcfletch at vrtelecom.com
Tue Aug 24 10:58:41 EDT 1999
Well, here's my take on a "better way"... I don't see why you would want to
use the RE module when you aren't doing any pattern matching...
def splitlen( instring, desiredlength):
temp = []
current = 0
lenstr = len( instring)
while current < lenstr:
newcurrent = current+desiredlength
temp.append( instring[current:newcurrent] )
current = newcurrent
return temp
I haven't even tested this for correctness, but I think it's close to
correct.
Hope this helps,
Mike
-----Original Message-----
From: python-list-request at cwi.nl [mailto:python-list-request at cwi.nl]On
Behalf Of MB
Sent: August 24, 1999 9:05 AM
To: python-list at cwi.nl
Subject: Newbie question about Re
Hi
I just want to split a string of n x (p characters) into a list of
p_length strings using the Re module
Example: Assume p=5 the string '12345ABCDE12345IJKLM' must be split in
['12345','ABCDE','12345','IJKLM'']
The way I find is to make a replace before splitting:
p=re.compile('(.{5,5})',re.DOTALL)
m=p.sub(r'\g<0>###','12345ABCDE12345IJKLM')
# I use ### as separator
re.split('###',m)
['12345', 'ABCDE', '12345', 'IJKLM', '']
# And I have to remove the trailing ''
But I don't like this too much.
Is there a better way?
Thanks
More information about the Python-list
mailing list