我有:
s='"Tag":"Football","name":"Mickael A","Played":"10times","Tag":"Basket","name":"Bruce B","Played":"8times","Tag":"Football","name":"John R","Played":"6times",'
我想根据足球和篮球进行分割,即:
['','"Mickael A","Played":"10times"',
'"Bruce B","Played":"8times",',
'"John R","Played":"6times",']
我尝试过:
s.strip().split(r'"Tag":("Football"|"Basket"),"name":')
但是它不起作用。
最佳答案
您需要的是使用 re
库并使 Football 和篮球非捕获组,这样它们就不会出现在结果中,如下所示:
import re
re.split(r'"Tag":(?:"Football"|"Basket"),"name":', s)
结果将是:
['', '"Mickael A","Played":"10times",', '"Bruce B","Played":"8times",', '"John R","Played":"6times",']
关于 python : string split with either A or B,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54928777/