Skip to content
Advertisement

Python: replace all characters in between two symbols that occur multiple times in a string

So I have a URL for a video file, and there is a simple hack I can do to get the audio file where I need to edit the string in a certain way.

Here is the starting string:

https://v.redd.it/jvjih3f894b61/DASH_1080.mp4?source=fallback

I need replace the resolution DASH_1080 to DASH_audio and I figured I could just replace everything between the _ after DASH and the . before .mp4 but there are multiple occurrences of . so using string.partition would not work.

The URL should look like this:

https://v.redd.it/jvjih3f894b61/DASH_audio.mp4?source=fallback

How could I do this, preferably without regex but I understand it might be the only way.

Thank you.

Advertisement

Answer

>>> s = 'https://v.redd.it/jvjih3f894b61/DASH_1080.mp4?source=fallback'
>>> re.sub('DASH_d+', 'DASH_audio', s)
'https://v.redd.it/jvjih3f894b61/DASH_audio.mp4?source=fallback'
Advertisement