擅长:python、mysql、java
<p>这似乎对我有用</p>
<pre><code>def list_to_regex(input, final=''):
if isinstance(input, list):
if all([isinstance(x,str) for x in input]):
# pure list found
y = ''.join(['|' if z == 'or' else z for z in input])
to_add = '(' + y + ')'
return to_add
else:
# mixed list
for el in input:
final += list_to_regex(el, final)
return '(' + final + ')'
else:
# just a string
if input == 'or':
return '|'
else:
return input
</code></pre>
<p>示例用法:</p>
<pre><code>l = [['mirna', 'or', ['hello', 'or', 'microrna']], 'or', ['lala', 'or','lele']]
# ((mirna|(hello|microrna))|(lala|lele))
</code></pre>