Python Regular Expression; replacing a portion of match

前端 未结 4 1262
时光说笑
时光说笑 2020-12-22 00:04

How would I limit match/replacement the leading zeros in e004_n07? However, if either term contains all zeros, then I need to retain one zero in the term (see example below

4条回答
  •  再見小時候
    2020-12-22 00:40

    There's no need to use re.sub if your replacement is so simple - simply use str.replace:

    s = 'e004_n07'
    s.replace('0', '') # => 'e4_n7'
    

提交回复
热议问题