使用RegEx列出的Python字符串

Puj*_*ava 1 python regex

我想将mystring转换为列表.

Input : "(11,4) , (2, 4), (5,4), (2,3) "
Output: ['11', '4', '2', '4', '5', '4', '2', '3']



>>>mystring="(11,4) , (2, 4), (5,4), (2,3)"
>>>mystring=re.sub(r'\s', '', mystring) #remove all whilespaces
>>>print mystring
(11,4),(2,4),(5,4),(2,3)

>>>splitter = re.compile(r'[\D]+')
>>>print splitter.split(mystring)
['', '11', '4', '2', '4', '5', '4', '2', '3', '']
Run Code Online (Sandbox Code Playgroud)

在此列表中,第一个和最后一个元素为空.(不必要的)

有没有更好的方法来做到这一点.

谢谢.

Ste*_*ski 8

>>> re.findall(r'\d+', "(11,4) , (2, 4), (5,4), (2,3) ")
['11', '4', '2', '4', '5', '4', '2', '3']
Run Code Online (Sandbox Code Playgroud)