擅长:python、mysql、java
<p>编辑:更正括号和“或”,我错过了上一个版本</p>
<p>一个简单的循环解决方案,也适用于多个斜杠(他/她/它/任何东西):</p>
<pre><code>def explode_versions(s):
match = re.search('^(.*?)(\S+)(?:(?:(?: or )|/)(\S+))+(.*?)$', s)
if match:
head, *versions, tail = match.groups()
versions[0] = re.sub('^\(', '', versions[0])
versions[-1] = re.sub('\)$', '', versions[-1])
return [line for v in versions for line in explode_versions(''.join([head, v, tail]))]
else:
return [s]
texts = ["someone can tell/figure",
"a/the squeaky wheel gets the grease/oil",
"accounts for (someone or something)",
"that's/there's (something/someone) for you"]
[explode_versions(text) for text in texts]
</code></pre>
<p>结果:</p>
<pre><code>[['someone can tell', 'someone can figure'],
['a squeaky wheel gets the grease',
'a squeaky wheel gets the oil',
'the squeaky wheel gets the grease',
'the squeaky wheel gets the oil'],
['accounts for someone', 'accounts for something'],
["that's something for you",
"that's someone for you",
"there's something for you",
"there's someone for you"]]
</code></pre>