Python中的正则表达式操作



如何得到我想要的?例如,我有一个这样的字符串

'RC00001  C00003_C00004RC00087  C00756_C01545RC01045  C06756_C03485'

我想得到

'RC00001  C00003_C00004','RC00087  C00756_C01545','RC01045  C06756_C03485' 

我该怎么办?我试过很多次,但都失败了。请帮帮我!谢谢你!

answer=[]
a="RC00001  C00003_C00004RC00087  C00756_C01545RC01045  C06756_C03485"
b = a.split("RC")
for i in b[1:]:
    answer.append("RC%s" % (i))
print(answer)

这将输出:

['RC00001 C00003_C00004', 'RC00087 C00756_C01545', 'RC01045 C06756_C03485']

如果您想使用regex实现这一点,您可以尝试下面的

import re
input_str = 'RC00001  C00003_C00004RC00087  C00756_C01545RC01045  C06756_C03485'
pattern = '(RC[d+]+s+C[d]+_C[d]+)'
print(re.findall(pattern, input_str))
# output
# [('RC00001  C00003_C00004', 'RC00087  C00756_C01545', 'RC01045  C06756_C03485')]

提供的格式总是RC{numbers} C{numbers}

最新更新