YouTube Link Finder task | Sololearn: Learn to code for FREE!


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 ( My code isn't passing the fourth test case. What have I lost in my pattern?

1/20/2020 12:34:35 AM


9 Answers

New Answer


I might have taken a shortcut 😬 inp = input() print(inp[-11:])


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.


Thanks to YouTube video ID being 11 characters, my code was very simple, passed all the tests: print(input()[-11:])


This was my answer: It’s pretty straightforward.


Solution by Manohar (optimized) import java.util.*; class Manoher { public static void main(String arg[]) { Scanner sc=new Scanner(; String accept=sc.nextLine(); //String accepted System.out.println(accept.substring(accept.length() -11)); } }


not optimized code import java.util.*; class YoutubeLinkFetcher { public static void main(String arg[]) { int no=0; String rev=""; Scanner sc=new Scanner(; 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()); } } } }


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::])


#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)


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()