Splitting a string every 'n'

Huaiyu Zhu huaiyu at gauss.almadan.ibm.com
Tue Jul 9 13:50:54 EDT 2002


Harvey Thomas <hst at empolis.co.uk> wrote:
>import re
>rex = re.compile('....', re.DOTALL)

To work with any int n, change to one of these

rex = re.compile('.{,%s}'%n, re.DOTALL)  # keeps remainder segment
rex = re.compile('.{%s}'%n, re.DOTALL)  # discards remainder segment

>
>for substring in rex.findall(string):
>	<process substring>



Huaiyu



More information about the Python-list mailing list