正则表达式 - 删除分隔符之间的字符串

时间:2018-03-08 11:08:15

标签: python regex

我有这个字符串

test_script_20180308.csv  
test_script_32343_121655_20180308_report.csv 

我能够使用'_2018' split()后截断字符串,从而导致'test_script''test_script_32343_121655'

如何使用re.sub()来实现它有困难。有什么想法吗? 另外,我如何裁切'test_script_32343_121655_20180308_report.csv' 'test_script_32343_121655_report.csv' '20180308'是动态约会?

1 个答案:

答案 0 :(得分:3)

我不明白你为什么需要正则表达式。简单的str.splitstr.join应该:

In [22]: str_ = 'test_script_32343_121655_20180308_report.csv'

# splitting on `_`
In [23]: splt = str_.split('_')

# joining the needed list elements with `_`
In [24]: '_'.join(splt[:4] + splt[5:])
Out[24]: 'test_script_32343_121655_report.csv'

如果你坚持使用Regex:

# match `_` followed by digits, with a positive lookahead
# to match `_` followed by a non-digit character
In [25]: re.sub(r'_\d+(?=_[^\d])', '', str_)
Out[25]: 'test_script_32343_121655_report.csv'