1.查找两个字符串中间的字符串
(?<=site=)(.*?)(?=&|$)表示位于‘site’与(‘&’或到结尾)之间的字符串
import re if __name__ == '__main__': line4='?uid=44236341&t=1465730505&parsemessage=2&source=1&token=ca4c0659eafee34b448e9d9022f94a75&authorid=43051171&device_id=8f65d346c2f23ffad1a8a1801ad91e430b&rel=1&tid=39779043&mode=2&version=5.0.0&siteflag=tlq' rn=None matchTid = re.search(r'(?<=site=)(.*?)(?=&|$)', line4, re.M | re.I) if matchTid: recommName=matchTid.group() if recommName: rn=recommName else: print line4 mt = re.search(r'(?<=siteflag=)(.*?)(?=&|$)', line4, re.M | re.I) if mt: rnn=mt.group() if rnn: rn=rnn print rn