s****h 发帖数: 3979 | 1 I have a string,'a1a2a3a4a5a6a', and want to extract all numbers:
['1', '2', '3', '4', '5', '6']
However, I can only get the odd numbers:
print re.findall('a(.+?)a' , 'a1a2a3a4a5a6a')
['1', '3', '5']
My guess:
non greedy re.findall could only find 'a1a' and get '1',
then do re.findall('a(.+?)a' , '2a3a4a5a6a'), find 'a3a' and get '3', and so
on.
Any suggestion? | m*********r 发帖数: 119 | 2 yes, because you add 'a' at last,
use re.findall('a(.+?)' , 'a1a2a3a4a5a6a') instead | D**u 发帖数: 288 | 3 只要数字的话re.findall('\d', 'a1a2a3a4a5a6a') 就好了吧 | s****h 发帖数: 3979 | 4 多谢回复
其实用re.split就行了。
这里的a,数字,其实都是一些字符串。
我是改自己以前的code,以前的那个case,pattern是'a(.+?)b'。前后的字符串不一样
。 | c*******n 发帖数: 679 | 5 'a1a2a3a4a5a6a'.split('a')[:-1]
so
【在 s****h 的大作中提到】 : I have a string,'a1a2a3a4a5a6a', and want to extract all numbers: : ['1', '2', '3', '4', '5', '6'] : However, I can only get the odd numbers: : print re.findall('a(.+?)a' , 'a1a2a3a4a5a6a') : ['1', '3', '5'] : My guess: : non greedy re.findall could only find 'a1a' and get '1', : then do re.findall('a(.+?)a' , '2a3a4a5a6a'), find 'a3a' and get '3', and so : on. : Any suggestion?
| l*******s 发帖数: 1258 | 6 实在想match的精确些 试试往前往后看
(?<='a')\d(?='a') |
|