擅长:python、mysql、java
<p>如果您想用“G | AATTC”替换“GAATTC”(不确定您最后想做什么),我认为您可以不用regex来处理这个问题:</p>
<pre><code>>>> string = 'GAATTCAAGAATTCTTGAATTCGAATTCAATATATA'
>>> string.replace('GAATTC', 'G|AATTC')
'G|AATTCAAG|AATTCTTG|AATTCG|AATTCAATATATA'
</code></pre>
<p>编辑:好的,这种方式可以根据您的需要进行调整:</p>
<pre><code>>>> groups = string.replace('GAATTC', 'G|AATTC').split('|')
>>> groups
['G', 'AATTCAAG', 'AATTCTTG', 'AATTCG', 'AATTCAATATATA']
>>> map(len, groups)
[1, 8, 8, 6, 13]
</code></pre>