Maison > Questions et réponses > le corps du texte
标题可能表达的不清楚,看下面的例子。
比如有一段字符串和一个名字列表:
string = "David comes from china. he loves watching tv shows. Tom is an american, he loves games. Rick blablabla" names = ["David", "Tom", "Rick"]
想写个函数,要return的结果:
r = ["David comes from china. he loves watching tv shows.", "Tom is an american, he loves games.", "Rick blablabla"]
不一定非要用正则。
三叔2016-11-11 09:38:18
string = "David comes from china. he loves watching tv shows. Tom is an american, he loves games. Rick blablabla" names = ["David", "Tom", "Rick"] import re print(re.split(r'(?<=\.)\s*(?=%s)' % '|'.join(names), string))