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.
CodePudding user response:
>>> 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'
CodePudding user response:
Use regular expressions and re.sub
:
s = "https://v.redd.it/jvjih3f894b61/DASH_1080.mp4?source=fallback"
re.sub(r"(?<=_). ?(?=\.)", "audio", s)
# 'https://v.redd.it/jvjih3f894b61/DASH_audio.mp4?source=fallback'
The two parenthesized expressions are positive lookahead/lookbehind assertions.
(?<=_). ?(?=\.)
non-greedily matches anything (". ?"
) between "_"
and "."
.
CodePudding user response:
I mean.. it's ugly and a regex is better but it'll likely work...
str = "https://v.redd.it/jvjih3f894b61/DASH_1080.mp4?source=fallback"
str_split = str.split("DASH_")
end_str = str_split[1].split(".")
str = str_split[0] "DASH_audio." ".".join(end_str[1:])
Output https://v.redd.it/jvjih3f894b61/DASH_audio.mp4?source=fallback