0

I'm attempting to make a 'check' to see if a given link matches a certain pattern. Here's what I have so far:

love = input("Enter URL: ")
while True:
    if love == 'https://www.youtube.com/watch?v=*':
        print("confirm, what is love?")
        break
    else:
        print("NOT A YOUTUBE LINK")

love is supposed to be a youtube link, starting in https://www.youtube.com/watch?v= and ending in 11 wildcard characters. How would one do this?

theboy
  • 353
  • 2
  • 10

4 Answers4

3

Use the startswith() method.

if love.startswith('https://www.youtube.com/watch?v='):
Barmar
  • 741,623
  • 53
  • 500
  • 612
3

If you need exactly 11 characters after the =, you could use regex:

import re

love = input("Enter URL: ")

if re.search(r"http://youtube.com/watch\?v=.{11}", love):
    print("Valid")
else:
    print("Invalid")

The .{11} in the regex pattern means match any character (.) exactly 11 times ({11}).

theasianpianist
  • 401
  • 4
  • 17
2

You don't need a while loop for the if statement like that. If you write it that way, your program will continuously print out "NOT A YOUTUBE LINK" because you have no break for else. If you put a break in your else statement, then there is no use for a while loop, since you stop the program after one try anyway. Also, use startswith() to check for the URL

If you want to use a loop, you can use it this way:

def checkURL(inputURL):
    if inputURL.startswith('https://www.youtube.com/watch?v='):
        print("confirm, what is love?")
    else:
        print("NOT A YOUTUBE LINK")

while True:
    love = input("Enter URL: ")
    if love.lower() != "quit":
        checkURL(love)
    else:
        break
Louis Ng
  • 21
  • 2
1

I see some problems with your code:

  1. you don't need the while loop, it will end your computer memory as long it is not a youtube link
  2. You can just check if youtube.com in the string variable.
  3. youtube domains can be **youtube.com`, youtu.be, and maybe some more that I don't know about. I suggest putting all of them in a list and check.
Asaf
  • 114
  • 5