Skip to main content
 首页 » 编程设计

python之如何在python中找到特殊字符之间的字符串

2025年05月04日23zengkefu

我有一组这样的字符串:

uc001acu.2;C1orf159;chr1:1046736-1056736;uc001act.2;C1orf159; 

我需要提取两个分号之间的子字符串,我只需要第一次出现。

结果应该是:C1orf159

我试过这段代码,但它不起作用:

import re 
info = "uc001acu.2;C1orf159;chr1:1046736-1056736;uc001act.2;C1orf159;" 
name = re.search(r'\;(.*)\;', info) 
print name.group() 

请帮帮我。 谢谢

请您参考如下方法:

您可以拆分字符串并将其限制为两次拆分。

x = info.split(';',2)[1]