【问题标题】:Regex - Match everything after second comma (or comma and space)正则表达式 - 匹配第二个逗号(或逗号和空格)之后的所有内容
【发布时间】:2022-01-05 17:28:09
【问题描述】:
【问题讨论】:
标签:
python
python-3.x
regex
【解决方案1】:
使用 re 的完整代码示例
-
[^,]+所有不带逗号的符号
-
, 逗号符号
-
[^,]+所有不带逗号的符号
-
, 逗号符号
-
\s* 0 个或多个空格
-
(.+) 有用的数据
import re
data="42: A: b41a2431, B: 7615239a, we, are(the champion 12 .)"
re.findall(r'[^,]+,[^,]+,\s*(.+)',data)
输出
['we, are(the champion 12 .)']
【解决方案2】:
你不能只使用 split() 和 join() 吗?
string = "42: A: b41a2431, B: 7615239a, we, are(the champion 12 .)"
result = ", ".join(string.split(", ")[2:])
print(result)
输出:
'we, are(the champion 12 .)'
【解决方案3】:
正则表达式对您不起作用,因为情况有点不同
该正则表达式假定字符串 starts 以 http(s)://
如果所有测试字符串都有 2 个逗号,那么正则表达式非常简单
.*?,.*?,(.+)
*?表示惰性(与贪婪)量词,因此它不会吃下一个逗号(因为它适合“任何字符”.)
你可以在这里看到演示https://regex101.com/r/XjLOVz/1