469,358 Members | 1,581 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,358 developers. It's quick & easy.

using regexp

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.

Mar 20 '07 #1
4 1123
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/Coo.../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?

s9************@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
sg*****@ncee.net | 402-438-8958 | http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy
Mar 20 '07 #2

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/Coo.../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?

s9************@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
sg*****@ncee.net | 402-438-8958 | http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy
Mar 20 '07 #3
On Mar 20, 1:57 pm, Shane Geiger <sgei...@ncee.netwrote:
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/Coo.../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...@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...@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.

Mar 20 '07 #4
On Mar 19, 10:33 pm, s99999999s2...@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.


Try:

import re

target_string = """not 123456789 but
try this line 987654321"""

try:
digits = re.compile(r'line\s+(\d
+)').search(target_string).group(1)
except AttributeError:
digits = ''

three_at_a_time = re.compile(r'\d{3}').findall(digits)
print three_at_a_time

--
Hope this helps,
Steven

Mar 20 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by David | last post: by
6 posts views Thread by Lukas Holcik | last post: by
5 posts views Thread by Lukas Holcik | last post: by
5 posts views Thread by Ones Self | last post: by
reply views Thread by Chris Croughton | last post: by
7 posts views Thread by Csaba Gabor | last post: by
2 posts views Thread by X l e c t r i c | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.