Python:使用A或B拆分字符串

时间:2019-02-28 15:12:06

标签: python regex

我有:

 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":')

但是它不起作用。

5 个答案:

答案 0 :(得分:2)

分析您的字符串,看来您需要:

re.findall(r'"name":(.*?),(?:"Tag"|$)', s)

其中s是您的字符串。这会找到某事(.*?)后跟"name":并先于,"Tag",<end>

的所有情况

完整代码

import re

s = '"Tag":"Football","name":"Mickael A","Played":"10times","Tag":"Basket","name":"Bruce B","Played":"8times","Tag":"Football","name":"John R","Played":"6times",'

print(re.findall(r'"name":(.*?),(?:"Tag"|$)', s))
# ['"Mickael A","Played":"10times"', '"Bruce B","Played":"8times"', '"John R","Played":"6times"']

答案 1 :(得分:2)

您需要使用re库,并将足球和篮球设为非捕获组,这样它们就不会出现在结果中:

import re
re.split(r'"Tag":(?:"Football"|"Basket"),"name":', s)

结果将是:

['', '"Mickael A","Played":"10times",', '"Bruce B","Played":"8times",', '"John R","Played":"6times",']

答案 2 :(得分:1)

您可以将以下正则表达式与re.split一起使用:

"Tag":"[^"]+","name":
  • "Tag":"字面匹配

  • [^"]+匹配一个或多个非"的字符,即匹配下一个"

  • ","name":字面匹配

您也可以使用非贪婪模式.*?"代替[^"]+

"Tag":".*?","name":'

示例:

In [486]: s = '"Tag":"Football","name":"Mickael A","Played":"10times","Tag":"Basket","name":"Bruce B","Played":"8times","Tag":"Football","name":"John R","Played":"6times",'

In [487]: re.split(r'"Tag":"[^"]+","name":', s)
Out[487]: 
['',
 '"Mickael A","Played":"10times",',
 '"Bruce B","Played":"8times",',
 '"John R","Played":"6times",']

In [488]: re.split(r'"Tag":".*?","name":', s)
Out[488]: 
['',
 '"Mickael A","Played":"10times",',
 '"Bruce B","Played":"8times",',
 '"John R","Played":"6times",']

答案 3 :(得分:0)

re库满足您的需求。

import re

s='"Tag":"Football","name":"Mickael A","Played":"10times","Tag":"Basket","name":"Bruce B","Played":"8times","Tag":"Football","name":"John R","Played":"6times",'
re.split('Football|Basket', s)

它返回

>>> ['"Tag":"',
     '","name":"Mickael A","Played":"10times","Tag":"',
     '","name":"Bruce B","Played":"8times","Tag":"',
     '","name":"John R","Played":"6times",'] 

答案 4 :(得分:0)

更好的方法是构造这个字符串,我假设玩过的名字和比赛(重复的涉及一个人)。在此字典列表之后,您可以轻松地操作数据

s='"Tag":"Football","name":"Mickael A","Played":"10times","Tag":"Basket","name":"Bruce B","Played":"8times","Tag":"Football","name":"John R","Played":"6times",'


l=[]
def fun(s):
 return str('{')+s+str('}')
import ast


k = s.strip().split(',')

for i in range(0,len(k),3):
    dic={}
    if len(k[i].split(':'))==2:
        dic['Tag']=ast.literal_eval(fun(k[i]))['Tag']
        dic['name']=ast.literal_eval(fun(k[i+1]))['name']
        dic['Played']=ast.literal_eval(fun(k[i+2]))['Played']
        l.append(dic)
print(l)
'''
output

[{'Tag': 'Football', 'name': 'Mickael A', 'Played': '10times'}, {'Tag': 'Basket', 'name': 'Bruce B', 'Played': '8times'}, {'Tag': 'Football', 'name': 'John R', 'Played': '6times'}]

'''