YouTube Link Finder task | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

YouTube Link Finder task

import re pattern = r"https://(www.)?[\w.]+/(watch\?v=)?([\w\d_]+)" url = input() match = re.match (pattern , url) if match: print (match.group(3)) My code isn't passing the fourth test case. What have I lost in my pattern?

20th Jan 2020, 12:34 AM
Hlad
Hlad - avatar
13 Answers
+ 16
I might have taken a shortcut 😬 inp = input() print(inp[-11:])
25th Jan 2020, 7:52 PM
Onuoha_ifeanyi
Onuoha_ifeanyi - avatar
+ 7
Instead of this, I would suggest you try this idea. We know youtube video id is starting after the v= and we also know that length of the id is 11 characters. just find the v in the link and after the v= collect 11 characters which will be the id of the YouTube video.
20th Jan 2020, 2:33 AM
Maninder $ingh
Maninder $ingh - avatar
+ 4
Thanks to YouTube video ID being 11 characters, my code was very simple, passed all the tests: print(input()[-11:])
7th Aug 2020, 3:18 PM
Bojan Adžić
Bojan Adžić - avatar
+ 1
This was my answer: https://code.sololearn.com/cK8wjT3bSESH/?ref=app It’s pretty straightforward.
5th Aug 2020, 1:26 PM
McInventor29
McInventor29 - avatar
+ 1
Easiest solution: url = input() if "=" in url: res = url.split("=") else: res =url.split("/") print(res[-1])
13th Oct 2022, 7:45 PM
mariusep
+ 1
url = input() if "watch" in url: print(url.replace("https://www.youtube.com/watch?v=" , "")) else: print(url.replace("https://youtu.be/" , ""))
17th Oct 2022, 4:16 PM
Mandeep sinh Gohil
Mandeep sinh Gohil - avatar
0
Solution by Manohar (optimized) import java.util.*; class Manoher { public static void main(String arg[]) { Scanner sc=new Scanner(System.in); String accept=sc.nextLine(); //String accepted System.out.println(accept.substring(accept.length() -11)); } }
24th Jan 2020, 7:58 AM
kartik narang
kartik narang - avatar
0
not optimized code import java.util.*; class YoutubeLinkFetcher { public static void main(String arg[]) { int no=0; String rev=""; Scanner sc=new Scanner(System.in); String str=sc.nextLine(); // string accepted here for(int i=str.length();i>0;--i) // { rev= rev+(str.charAt(i-1)); // String has been reversed no++; if(no==11) { //System.out.println(rev); StringBuilder xx=new StringBuilder(rev); System.out.println(xx.reverse()); } } } }
24th Jan 2020, 7:59 AM
kartik narang
kartik narang - avatar
0
import re pattern = r"https://(www.)?[\w.]+/(watch\?v=)?([\w\d_]+)" url = input() match = re.match (pattern , url) if match: print (match.group(3)) #print(match.group[-11:]) #print(input()[-11:])
13th Nov 2022, 6:25 PM
Anita chaubey
Anita chaubey - avatar
0
import re pattern = r"https://(www.)?[\w.]+/(watch\?v=)?([\w\d_]+)" url = input() match = re.match (pattern , url) if match: print (match.group(3)) #print(match.group[-11:]) #print(input()[-11:])
13th Nov 2022, 6:25 PM
Anita chaubey
Anita chaubey - avatar
- 1
This is my solutions url = str(input()) s = url.replace("/","").replace(":"," ").replace("="," ").replace("."," ") ID = s.split() if len(ID) >3: print(ID[4]) else: print(ID[2][2::])
16th Dec 2021, 12:57 AM
Samnith Vath
Samnith Vath - avatar
- 2
file = open("/usercode/files/books.txt", "r") #your code goes here readfile=file.readlines() for i in readfile: sv=i.split('\n') sv1= sv[0] sv2=sv[0][0] sv3=str(len(sv1)) print(sv2+sv3) file.close()
5th Dec 2020, 10:35 AM
Md.Riad Hossen
Md.Riad Hossen - avatar
- 3
#The Simplest answer 😁 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:41 AM
Jahir Raihan Joy
Jahir Raihan Joy - avatar