<div>
</div>
<p>我有一个模板构建的文本行:</p>
<ol>
<li>这首歌的名字(歌手)(歌年)</李>
<li>这首歌的名字(当时是歌手(歌年))</李>
</ol>
<p>模板之间的区别在于歌手括号内或括号外的歌曲年份</p>
<p>我想把每一行分成三部分:</p>
<ol>
<li>歌名</li>
<li>歌手</li>
<li>宋年</li>
</ol>
<p><strong>小示例</strong></p>
<pre><code>Ring Ring (ABBA (1973))
Waterloo (ABBA) (1974)
If I Don’t Write This Song Someone I Love Will Die (Hello Saferide) (2005)
My Best Friend (Hello Saferide (2005))
</code></pre>
<p>我尝试将RexExp与逻辑OR一起使用</p>
<pre class="lang-py prettyprint-override"><code>import re
the_lines = ("Ring Ring (ABBA (1973))",
"Waterloo (ABBA) (1974)",
"If I Don’t Write This Song Someone I Love Will Die (Hello Saferide) (2005)",
"My Best Friend (Hello Saferide (2005))",
)
pattern = r"((.*) \((.*)\) \((\d*)\))|((.*) \((.*\((\d*)\))\))"
for line in the_lines:
title, artist, year = re.split(pattern, line)
print(title, artist, year)
</code></pre>
<p>但是这个结果是<a href="https://pythex.org/?regex=((.*)%20%5C((.*)%5C)%20%5C((%5Cd*)%5C))%7C((.*)%20%5C((.*%5C((%5Cd*)%5C))%5C))&test_string=Ring%20Ring%20(ABBA%20(1973))%0A%0AWaterloo%20(ABBA)%20(1974)%0A%0AIf%20I%20Don%E2%80%99t%20Write%20This%20Song%20Someone%20I%20Love%20Will%20Die%20(Hello%20Saferide)%20(2005)%0A%0AMy%20Best%20Friend%20(Hello%20Saferide%20(2005))&ignorecase=0&multiline=0&dotall=0&verbose=0" rel="nofollow noreferrer">redundant</a>,它得到8个组</p>
<p>您的规范并不真正需要REs,对于每一行,看起来您可以使用<code>artist_song_year = line.split("(")</code>,然后使用额外的清理步骤,如<code>artist_song_year = [item.strip(")").strip(")") for item in artist_song_year]</code></p>