regex - Find string with regular expression in python -


i newbie in python , trying cut piece of string in string @ python. looked @ other similar questions not find answer.

i have variable contain domain list domains :

http://92.230.38.21/ios/channel767/hotbird.mp3 http://92.230.38.21/ios/channel9798/coldbird.mp3

....

i want mp3 file name (in example hotbird, coldbird etc)

i know must able re.findall() have no idea regular expressions need use.

any idea?

update: here part used:

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

which did not work. tried 1 cut ip address (92.230.28.21) not name:

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

you may use str.split():

>>> urls = ["http://92.230.38.21/ios/channel767/hotbird.mp3", "http://92.230.38.21/ios/channel9798/coldbird.mp3"] >>> url in urls: ...     print(url.split("/")[-1].split(".")[0]) ...  hotbird coldbird 

and here example regex-based approach:

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

where using capturing group (\w+) capture mp3 filename consisting of 1 or more aplhanumeric characters followed dot, mp3 @ end of url.


Comments