locked
YouTube video URL Regex bug... RRS feed

  • Question

  • Hello!

    I was trying the other day, to make a Regular expression, which validates the user specified YouTube URL.
    This is the current Regex:

    "^(http\:\/\/)?(www\.)?(?(?=youtu\.be)\/[\w-]{11}|(([a-z]{2,3}\.)?(youtube\.com))\/((watch(\?|\#)(?:v\=))|(v\/)|(watch\?))([\w-]{11})(?(?=\&).*?))$" 
    

    It doesn't work that well, and bugs at times.
    These are the example URL's I tested out:

    ---------------------------------------------------------------------------

    http://www.youtube.com/watch?v=XHH6NMyxjuU - SUCCESS

    http://www.youtube.com/watch#v=XHH6NMyxjuU - SUCCESS

    http://www.youtube.com/v/XHH6NMyxjuU - SUCCESS

    http://youtube.com/watch?v=XHH6NMyxjuU - SUCCESS

    www.youtube.com/watch?v=XHH6NMyxjuU - SUCCESS

    youtube.com/watch?v=XHH6NMyxjuU - SUCCESS

    youtube.com/v/XHH6NMyxjuU - SUCCESS

    http://www.youtube.com/watch?v=XHH6NMyxjuU - SUCCESS

    http://www.youtube.com/watch?featured=idk&v=XHH6NMyxjuU - FAILED

    http://www.youtube.com/watch?v=XHH6NMyxjuU&fe - SUCCESS

    http://www.youtube.com/watch?v=XHH6NMyxjuU&featured=idk - FAILED

    http://www.youtu.be/XHH6NMyxjuU - FAILED

    http://youtu.be/XHH6NMyxjuU - FAILED

    http://www.youtube.com/watch? - FAILED

    http://www.youtube.com/watch?v=XHH6NMyxjuUd - FAILED

    http://www.youtube.com/v=XHH6NMyxjuU - FAILED

    http://www.youtube.com/watch?v= - FAILED

    ---------------------------------------------------------------------------

    If you could tell me what's wrong with the Regex, it would be awesome :)

    Thank you in advice
    - Jonas T. 


    Jonas
    • Moved by Martin_Xie Monday, August 1, 2011 9:27 AM Redirect to regular expression forum for more effective support. (From:Visual Basic General)
    Tuesday, July 26, 2011 8:48 PM

Answers

All replies