using regexp

S

s99999999s2003

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.
 
S

Shane Geiger

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?



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
(e-mail address removed) | 402-438-8958 | http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy
 
S

Shane Geiger

import re
line = '123456789123456789'
print re.findall('([0-9]{3})', line)



Shane said:
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?



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
(e-mail address removed) | 402-438-8958 | http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy
 
S

s99999999s2003

import re
line = '123456789123456789'
print re.findall('([0-9]{3})', line)



Shane said:
You don't even need regex.

# this is sort of the inverse of
flatten
return [seq[i:i+size] for i in range(0, len(seq),
size)]
line =
'123456789123456789'

Will that work for you?
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
(e-mail address removed) | 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.
 
A

attn.steven.kuo

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,786
Messages
2,569,626
Members
45,323
Latest member
XOBJamel3

Latest Threads

Top