0

I have a string similar to the following

circles_b2_test_20130206_rem01_toy.jpg

I'd like it to look like

circles_b2_test_20130206_toy.jpg

How can I remove only the "rem01" phrase? The numbers after rem are always changing but I always want to remove "rem" + 2 characters.

franchyze923
  • 1,060
  • 2
  • 12
  • 38

2 Answers2

2

A bit "hacky," but this should achieve what you're after if there are always 2 numbers after the rem

str = "circles_b2_test_20130206_rem01_toy.jpg"
idx = str.index("rem")
new_str = str[:idx] + str[idx+6:]
Dor-Ron
  • 1,787
  • 3
  • 15
  • 27
2

If it's rem + two characters then you can use rem.{2}.

If it's rem + two digits then you can use rem\d{2}.

Note if you want circles_b2_test_20130206_toy.jpg then you need to check the _ in front of 01, you could try with:

import re
file = 'circles_b2_test_20130206_rem01_toy.jpg'
re.sub(r'rem\d{2}_?', '', file)
# 'circles_b2_test_20130206_toy.jpg'
Sebastián Palma
  • 32,692
  • 6
  • 40
  • 59