擅长:python、mysql、java
<p>我不会在这里使用正则表达式。简单的字符串拆分和格式化就可以了</p>
<pre><code>h = '>ref|NC_001133| [org=Saccharomyces cerevisiae] [strain=S288C] [moltype=genomic] [chromosome=I]'
#h = '>ref|NC_001224| [org=Saccharomyces cerevisiae] [strain=S288C] [moltype=genomic] [location=mitochondrion] [top=circular]'
fields = h[1:].split() #[1:] so you skip the >
if len(fields) == 6:
new_h = [fields[-1]] + fields[1:-1] + [f'[{fields[0]}]']
elif len(fields) == 7: #extra filed for the mitocondrion case
new_h = [fields[-2]] + fields[1:-2] + [fields[-1]] + [f'[{fields[0]}]']
new_h = f'> {" ".join(new_h)}'
print(new_h)
> [chromosome=I] [org=Saccharomyces cerevisiae] [strain=S288C] [moltype=genomic] [ref|NC_001133|]
</code></pre>