using regexp

Shane Geiger sgeiger at ncee.net
Tue Mar 20 01:51:02 EDT 2007


You don't even need regex.

def 
split_seq(seq,size):                                                                                                               

    # this is sort of the inverse of 
flatten                                                                                           

    # Source: 
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/425044                                                           

    return [seq[i:i+size] for i in range(0, len(seq), 
size)]                                                                           

                                                                                                                                       

line = 
'123456789123456789'                                                                                                            

                                                                                                                                       

print 
split_seq(line,3)                                                                                                                



Will that work for you?



s99999999s2003 at yahoo.com wrote:
> hi
> how can i use regexp to group these digits into groups of 3?
>
> eg
> line 123456789123456789
>
> i have :
>
> pat = re.compile("line\s+(\d{3})" , re.M|re.DOTALL)
>
> but this only gives the first 3. I also tried
>
> "line\s+(\d{3})+"
> but also not working.
> I need output to be ['123' ,'456','789', '123','456','789', .....]
> thanks.
>
>   

-- 
Shane Geiger
IT Director
National Council on Economic Education
sgeiger at ncee.net  |  402-438-8958  |  http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy

-------------- next part --------------
A non-text attachment was scrubbed...
Name: sgeiger.vcf
Type: text/x-vcard
Size: 310 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20070320/7e147968/attachment.vcf>


More information about the Python-list mailing list