4

I am a newbie in python and I am trying to cut piece of string in another string at python. I looked at other similar questions but I could not find my answer.

I have a variable which contain a domain list which the domains look like this :

http://92.230.38.21/ios/Channel767/Hotbird.mp3 http://92.230.38.21/ios/Channel9798/Coldbird.mp3

....

I want the mp3 file name (in this example Hotbird, Coldbird etc)

I know I must be able to do it with re.findall() but I have no idea about regular expressions I need to use.

Any idea?

Update: Here is the part I used:

    for final in match2:
         netname=re.findall('\W+\//\W+\/\W+\/\W+\/\W+', final)
         print final
         print netname

Which did not work. Then I tried to do this one which only cut the ip address (92.230.28.21) but not the name:

    for final in match2:
         netname=re.findall('\d+\.\d+\.\d+\.\d+', final)
         print final
1
  • 1
    Can you show any of the code you have already tried? Commented Sep 26, 2015 at 1:07

2 Answers 2

5

You may just use str.split():

>>> urls = ["http://92.230.38.21/ios/Channel767/Hotbird.mp3", "http://92.230.38.21/ios/Channel9798/Coldbird.mp3"]
>>> for url in urls:
...     print(url.split("/")[-1].split(".")[0])
... 
Hotbird
Coldbird

And here is an example regex-based approach:

>>> import re
>>>
>>> pattern = re.compile(r"/(\w+)\.mp3$")
>>> for url in urls:
...     print(pattern.search(url).group(1))
... 
Hotbird
Coldbird

where we are using a capturing group (\w+) to capture the mp3 filename consisting of one or more aplhanumeric characters which is followed by a dot, mp3 at the end of the url.

Sign up to request clarification or add additional context in comments.

Comments

0

How about ?

([^/]*mp3)$

I think that might work

Basically it says...

Match from the end of the line, start with mp3, then match everything back to the first slash.

Think it will perform well.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.