Reputation: 395
I'm working on a project which converts youtube playlist to spotify playlist. I'm done with the youtube api to get the title of songs and with spotify playlist to add songs in a playlist.
The problem is i have to get the artist name and track name from a title like Zedd & Kehlani - Good Thing (Official Music Video)
. I tried to do it with youtube dl
extract metadata method, but it doesnt seem to work most of the time. Luckily i found a module with gives the artist and track name but this also returns more than one artist if they are mentioned on the title. But i can only search with one artist name on spotify.
So, i need help with finding a regex which will only return one artist if there are more than one present in a string. I came up with this regex -
artist_pattern = re.compile(r'([\w-]+\s?[\w]*)\s?([\w\s]+)?')
matches = artist_pattern.finditer(names)
for match in matches :
print(match.group(1))
This works perfectly in some cases, but fails if name = ABC feat. XYZ
which prints out ABC featXYZ
.
I know reading regex off internet can be tough (especially written by a beginner like me :) ), so im doing my best to explain -
[\w-]+ ##matches chars and - for artist name
\s? ## if the artist has a space between his name (eg - DJ Snake) ***this is where i think this regex fails***
[\w]* ## if the artist name has a space this will record the chars after the space
\s ## a space if the title has more than one artist
([\w\s]+)? ## if the title has more than one artist this will record the left artists
As i mentioned before this doesn't work with test cases like -
Anne-Marie & James Arthur ## result - Anne-Marie James Arthur
Calvin Harris, Rag'n'Bone Man ##result - Calvin HarrisnBone Man
DJ Snake feat. J Balvin ## result - DJ SnakeJ Balvin
and similar test cases.
Now i know i can mention these symbols (&
, ,
, feat
, ft.
) explicitly in regex but this solution will work only with some number of titles. If the title is deviates from the regex. Then the code fails.
Any help is appreciated, Thanks
Upvotes: 0
Views: 497
Reputation: 5730
text = 'Anne-Marie & James Arthur'
text = "Calvin Harris, Rag'n'Bone Man"
text = 'DJ Snake feat. J Balvin'
text = 'ABC feat. XYZ'
re.search('\w+.\w+', text).group(0).replace(' feat', '')
Output
Anne-Marie
Calvin Harris
DJ Snake
ABC
Upvotes: 1