tags:

views:

68

answers:

2

hi i have a string like this

track._Event('product', 'test');Product.lisen(1234, 21, 4343); return false;

i want to use some regular expression so i would end up with groups

pid = 1234
p1 = 21
p2 = 4343
+2  A: 
import re

s = "track._Event('product', 'test');Product.lisen(1234, 21, 4343); return false;"

pattern = re.compile(r'.*lisen\((?P<pid>\d+),\s*(?P<p1>\d+),\s*(?P<p2>\d+)\).*')

pid, p1, p2 =  map(int, pattern.match(s).groups())

Note: I used named capturing groups, but that is not necessary in this case.

awesomo
thanks this is what i want to do all along
nabizan
A: 

Why regular expression? you can do it will simple string manipulations

>>> s="track._Event('product', 'test');Product.lisen(1234, 21, 4343); return false;"
>>> s.split("lisen(")[-1].split(")")[0].split(",")
['1234', ' 21', ' 4343']
ghostdog74