Newbie question about Re

Tim Peters tim_one at email.msn.com
Tue Aug 24 20:53:13 EDT 1999


[MB]
> 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'']
> ...

You're much better off (somewhat clearer in the normal case, much clearer in
end cases, and much faster regardless) using string-slicing in a loop, as
others have suggested.

If you've been cursed by a powerful wizard such that you must use re for this
or be turned into a toad,

>>> import re
>>> def chunk(s, p):
        return re.findall("." * p, s)

>>> chunk("12345ABCDE12345IJKLM", 5)
['12345', 'ABCDE', '12345', 'IJKLM']
>>> chunk("12345ABCDE12345IJKLM", 4)
['1234', '5ABC', 'DE12', '345I', 'JKLM']
>>> chunk("12345ABCDE12345IJKLM", 1)
['1', '2', '3', '4', '5', 'A', 'B', 'C', 'D', 'E',
 '1', '2', '3', '4', '5', 'I', 'J', 'K', 'L', 'M']
>>> chunk("12345ABCDE12345IJKLM", 0)
['', '', '', '', '', '', '', '', '', '', '',
 '', '', '', '', '', '', '', '', '', '']
>>> chunk("12345ABCDE12345IJKLM", 17)
['12345ABCDE12345IJ']
>>> chunk("12345ABCDE12345IJKLM", 10)
['12345ABCDE', '12345IJKLM']
>>> chunk("12345ABCDE12345IJKLM", 20)
['12345ABCDE12345IJKLM']
>>> chunk("12345ABCDE12345IJKLM", 21)
[]
>>>

being-a-toad-ain't-so-bad-ly y'rs  - tim






More information about the Python-list mailing list