怎样用re.findall()来对字符串分组呢?在stackoverflow上找到了答案http://stackoverflow.com/questions/6018340/capturing-group-with-findall
举个栗子:
>>> re.findall('abc(de)fg(123)', 'abcdefg123 and again abcdefg123') [('de', '123'), ('de', '123')]
这样得到了结果,接着怎样在这个list中迭代呢?lz写了个栗子:
#coding:utf-8 import re line = '<img src="/files/attachments/00138729035993893cc9f9690e042848b0f7e1816815a36000/0" alt="我是图片">' listtup = re.findall(r'<img src="(.*?)" alt="(.*?)"',line) for src,alt in listtup: print src print alt
结果是:
/files/attachments/00138729035993893cc9f9690e042848b0f7e1816815a36000/0
我是图片