user12105861
user12105861

Reputation:

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.

Upvotes: 0

Views: 505

Answers (3)

Brodie
Brodie

Reputation: 67

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

Upvotes: 0

Antony Hatchkins
Antony Hatchkins

Reputation: 34004

>>> 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'

Upvotes: 1

user2390182
user2390182

Reputation: 73470

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 ".".

Upvotes: 0

Related Questions