using regexp

s99999999s2003 at yahoo.com s99999999s2003 at yahoo.com
Tue Mar 20 07:08:29 CET 2007


On Mar 20, 1:57 pm, Shane Geiger <sgei... at ncee.net> wrote:
> import re
> line = '123456789123456789'
> print re.findall('([0-9]{3})', line)
>
>
>
> Shane Geiger wrote:
> > 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?
>
> > s99999999s2... 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
> sgei... at ncee.net  |  402-438-8958  |  http://www.ncee.net
>
> Leading the Campaign for Economic and Financial Literacy
>
>  sgeiger.vcf
> 1KDownload


hi
thanks.
I know it can be done without regexp, but its just for learning
purpose, i want to use regexp
eg
someword 123456789123456789 #this is a line of a file. 'someword' is a
word before the digits
I need to get the digits into groups of 3 based on whether i found
'someword'.
so i use  this pattern :
"someword\s+(\d{3})"
but this will on give me the first 3.  I need to group them all.
hope i explain it clearly.




More information about the Python-list mailing list