使用正则表达式替换长字符串

时间:2015-03-07 23:19:00

标签: python regex

我想替换字符串

ID12345678_S3_MPRAGE_ADNI_32Ch_2_98_clone_transform_clone_reg_N3Corrected1_mask_cp_strip_durastripped_N3Corrected_clone_lToads_lesions_seg

ID12345678

如何通过正则表达式替换它?

我试过了 - 它没用。

import re
re.sub(r'_\w+_\d_\d+_\w+','')

谢谢

2 个答案:

答案 0 :(得分:1)

您可以使用re.sub模式[^_]*,其匹配文本中不包含_的任何子字符串,而re.sub替换首次匹配模式在这种情况下使用它:

>>> s="ID12345678_S3_MPRAGE_ADNI_32Ch_2_98_clone_transform_clone_reg_N3Corrected1_mask_cp_strip_durastripped_N3Corrected_clone_lToads_lesions_seg"
>>> import re
>>> re.sub(r'([^_]*).*',r'\1',s)
'ID12345678'

但如果它可以出现在您的字符串中的任何位置,则可以使用re.search,如下所示:

>>> re.search(r'ID\d+',s).group(0)
'ID12345678'
>>> s="_S3_MPRAGE_ADNI_ID12345678_32Ch_2_98_clone_transform_clone_reg_N3Corrected1_mask_cp_strip_durastripped_N3Corrected_clone_lToads_lesions_seg"
>>> re.search(r'ID\d+',s).group(0)
'ID12345678'

但如果没有正则表达式,您可以使用split()

>>> s.split('_',1)[0]
'ID12345678' 

答案 1 :(得分:0)

我猜第一部分是可变的,然后是

import re
s = "ID12345678_S3_MPRAGE_ADNI_32Ch_2_98_clone_transform_clone_reg_N3Corrected1_mask_cp_strip_durastripped_N3Corrected_clone_lToads_lesions_seg"
print re.sub(r'_.*$', r'', s)