using regexp

attn.steven.kuo at gmail.com attn.steven.kuo at gmail.com
Tue Mar 20 02:26:25 EDT 2007


On Mar 19, 10:33 pm, 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.



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




More information about the Python-list mailing list