+ 5

Youtube Link Finder - Python3 - ReGeX

Im trying to solve this problem, my code is below: import re inn = input() pattern = r"\b[=/](\w+)" match = re.findall(pattern,inn) if len(match): print(match[-1]) Test case #4 is failing and i dont know why, does someone know what i may be doing wrong? https://sololearn.com/coach/74/?ref=app

25th Aug 2020, 6:09 PM
Enrique Salinas
Enrique Salinas - avatar
7 Antworten
+ 5
Kike Salinas To be honest, I have a very poor knowledge about regex. But let me try, I read your regex and it's super smart(I like it) but do you think that the youtube video ID only got alphabets, numeric and underscore symbol in them? [ (\w+) - > Returns a match where the string contains any word characters (characters from a to Z, digits from 0-9, and the underscore _ character)] src: w3schools Because along with reading your code, I also did a small research on YouTube video ID and I found that some ID also have got a dash(-), ampersand(&) and many others symbol. (I may be wrong, sorry in advance) 😇 (by the way your code is working perfect with YouTube video ID having alphabets, numeric, underscore in them) (and I'm not 100% though that this is the reason for test case 4 failure)
25th Aug 2020, 6:41 PM
minirkk
minirkk - avatar
+ 3
Kike Salinas I have recently started programming in Python. Here is my code link = list(input()) n = len(link) id = [] for i in range(n - 10, n + 1): id.append(link[i - 1]) print(''.join(id))
26th Aug 2020, 7:17 PM
Konstantin K
Konstantin K - avatar
+ 2
Konstantin K read Terminal_Phantom solution, its a simpler way to do what you are doing (both are correct) but in this case i was trying to find out a solution using ReGeX.
27th Aug 2020, 11:43 AM
Enrique Salinas
Enrique Salinas - avatar
+ 1
rkk thx, you were right! i modified pattern as shown: pattern = r"\b[/=]([^/=]+)" and it works ! btw the problem said: “Keep the video ID (the combination of letters and numbers at the end of the link) “ and i used \w cause in the examples of the problem there r also underline characters. appreciate your help!
25th Aug 2020, 7:20 PM
Enrique Salinas
Enrique Salinas - avatar
+ 1
My solution : link = input() print(link[-11:])
25th Aug 2020, 7:53 PM
Terminal_Phantom
Terminal_Phantom - avatar
0
How about the second kind of the input? You could first save your code here in Playground and check how it works for both different inputs.
25th Aug 2020, 6:26 PM
JaScript
JaScript - avatar
0
#Can i have a vote for not using regex😂 #my code link =input('') if '=' in link : lf1 = link.split('=') d = lf1[-1] print(d) else: lf2= link.split('/') m = lf2[-1] print(m)
24th Sep 2020, 3:44 AM
Jahir Raihan Joy
Jahir Raihan Joy - avatar