python : string split with either A or B

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

但是它不起作用。

最佳答案

您需要的是使用 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/

相关文章:

php - 正则表达式从字符串中删除主题标签

javascript - 用于验证字符和数字的正则表达式

c# - 将正则表达式转换为 SQL

python - 键盘控制 Python

python - 无法让导入在 web.py 项目中工作

python - Pandas 删除字符串中的部分空格

regex - 匹配数字中的增量数字

python - 将两个指数不匹配的 Pandas 系列相乘

python - Docopt 单破折号参数,不带空格

python - 在 greenlet 中访问 flask.g