Reputation: 2194
I'm trying to achieve the following:
string = 'C:/some path to mp3/song (7) title and so on (1).mp3'
should become:
C:/some path to mp3/song (7) title and so on.mp3
To match it i'm using the following regex:
pattern = '.*(\s\([0-9]+\))\.mp3'
And the match group contains: (u' (1)',)
however, when i'm trying to substitute the match like so:
processed = re.sub(pattern, '', string)
processed contains an empty string. How can i get re.sub() to only replace the match found above?
Upvotes: 1
Views: 190
Reputation: 20486
You were matching the entire string and replacing it, use a lookahead and only match the whitespace and (1)
before the final extension.
Expanded RegEx:
\s* (?# 0+ characters of leading whitespace)
\( (?# match ( literally)
[0-9]+ (?# match 1+ digits)
\) (?# match ) literally)
(?= (?# start lookahead)
\. (?# match . literally)
mp3 (?# match the mp3 extension)
$ (?# match the end of the string)
) (?# end lookeahd)
Demo: Regex101
Implementation:
pattern = '\s*\([0-9]+\)(?=\.mp3$)'
processed = re.sub(pattern, '', string)
Notes:
mp3
can be replaced by [^.]+
to match any extension or (mp3|mp4)
to match multiple extensions.\s+
instead of \s*
to require at least some whitespace before (1)
, thanks @SethMMorton.Upvotes: 2