python正则表达式歌曲

2024-06-17 11:17:14 发布

您现在位置:Python中文网/ 问答频道 /正文

抱歉,我需要一些帮助来找到正确的正则表达式。 基本上,我想通过regex识别以下格式是否为字符串:

艺术家头衔(某物)

示例:

"ellie goulding - good gracious (the chainsmokers remix)"
"the xx - you got the love (florence and the machine cover)"
"neneh cherry - everything (loco dice remix)"
"my chemical romance - famous last words (video)"

我一直在尝试,但没有找到正确的正则表达式。你知道吗

regex = "[A-Za-z0-9\s]+[\-]{1}[A-Za-z0-9\s]+[\(]{1}[\s]*[A-Za-z0-9\s]*[\)]{1}"

救命啊!你知道吗


Tags: the字符串示例格式regexgood艺术家头衔
2条回答

我会的

regex = r"^[^\-]*-[^\(]*\([^\)]*\)$"
>>> import re
>>> songstring = 'ellie goulding - good gracious (the chainsmokers remix)'
>>> re.match(r'(.*?) - (.*?) (\(.*?\))', songstring).groups()
('ellie goulding', 'good gracious', '(the chainsmokers remix)')

相关问题 更多 >