2

I am using the YouTube embedded link in my website .I want to validate the link as if user paste something else other then embedded link then it should give me an alert invalid URL. I have used so many regex some has already in my code i have commented it .I want regular expression of YouTube embedded link only. Here i my code:

package com.edubot.client.lecture;

import gwt.material.design.client.ui.MaterialButton;
import gwt.material.design.client.ui.MaterialTextBox;
import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Widget;

public class EmbeddedLink extends Composite  {

    private static EmbeddedLinkUiBinder uiBinder = GWT
            .create(EmbeddedLinkUiBinder.class);

    interface EmbeddedLinkUiBinder extends UiBinder<Widget, EmbeddedLink> {
    }

    @UiField MaterialButton buttonembedded;
    //  @UiField IFrameElement youtubevideo;
    @UiField HTMLPanel htmlpanel;
    @UiField MaterialTextBox textbox ;



    public EmbeddedLink() {
        super();
        sinkEvents( Event.ONPASTE );
        initWidget(uiBinder.createAndBindUi(this));

    }

    @Override
    public void onBrowserEvent(Event event) {
        super.onBrowserEvent(event);
        switch (event.getTypeInt()) {
        case Event.ONPASTE: {
            Timer timer  = new Timer() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    onPasted();
                    Window.alert("paste");
                }
            };
            timer.schedule(1000);
        }
        }

    }

    //  @UiHandler("buttonembedded")
    //      void onClick(ClickEvent e) { 
    ////        onPasted(); 
    //      }


    private void onPasted(){
        //      youtubevideo.setSrc(addEmbeddedLink());
        Window.alert("msg1");
        if(testEmbeddedLink()) {
            String link=textbox.getText().trim();
            htmlpanel.getElement().setInnerHTML(link);
            Window.alert("Valid URL");
        } else {
            Window.alert("Invalid URL");
        }


    }


    public boolean testEmbeddedLink(){
        String link=textbox.getText().trim();
        Window.alert("msg");
        String patternString = "(?:http(?:s)?:\/\/)?(?:www\.)?(?:m\.)?(?:youtu\.be\/|youtube\.com\/)(?:)(?:.*&)?v(?:i)?=|(?:embed|v|vi|user)\/[a-zA-Z0-9\-]*";
        //      String patternString = "<iframe title='YouTube video player' width='' height='' src='http://www.youtube.com/embed/$1' frameborder='0' allowfullscreen='1'></iframe>";
        //      String patternString = "~<iframe.+?src="https?://www.youtube.com/embed/([a-zA-Z0-9_-]{11})"[^>]+?></iframe>~i";
        boolean result = link.matches(patternString);

        return result;

    }
    //  "youtube.com/(?<=v|embed\\)?[a-zA-Z0-9]+[^#\\&\\?]*";
    //  "(?<=youtu.be/?<=v|embed\\/)?[a-zA-Z0-9]+[^#\\&\\?]*";
    //  "(https?://)?(www\\.)?(yotu\\.be/|youtube\\.com/)?((.+/)?(watch(\\?v=|.+&v=))?(v=)?)([\\w_-]{11})(&.+)?"
    //  (\"http:\/\/www\.youtube\.com\/v\/\w{11}\&rel\=1\");
    //  (https?://www.youtube(?:-nocookie)?.com/(?:v|embed)/([a-zA-Z0-9-]+).*)"
    //  /<iframe.+?src="http://www.youtube.com/embed/([a-zA-Z0-9_-]{11})"[^>]+?>";</iframe>/i";
    //   "(?:youtube.com)\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})\W";
    //  "s*(https?://www.youtube(?:-nocookie)?.com/(?:v|embed)/([a-zA-Z0-9-]+).*) ";
//   "^.*((youtu.be"+ "\\/)" + "|(v\\/)|(\\/u\\/w\\/)|(embed\\/)|(watch\\?))\\??v?=?([^#\\&\\?]*)

}
  • 1
    Possible duplicate of [Regular expression for youtube links](http://stackoverflow.com/questions/3717115/regular-expression-for-youtube-links) – Jelmergu May 18 '17 at 07:19
  • yes,but they are not working in my code as they all are for YouTube video,URL and embedded but i want only for embedded link only.. – Shalini Upadhyay May 18 '17 at 07:33
  • and the line `String patternString = "(?:http(?:s)?:\/\/)?(?:www\.)?(?:m\.)?(?:youtu\.be\/|youtube\.com\/)(?:)(?:.*&)?v(?:i)?=|(?:embed|v|vi|user)\/[a-zA-Z0-9\-]*";` does not work propperly? Seems to me that it should perform the job, or is it to broad – Jelmergu May 18 '17 at 07:46
  • it gives an error on this potion when i am pasting it "(?:http(?:s)?:\/ – Shalini Upadhyay May 18 '17 at 08:08
  • How about using `https?://(?:www)\.youtu(?:be\.com|\.be)/embed/[a-zA-Z0-9\-]*` which is slightly stripped down version – Jelmergu May 18 '17 at 08:25
  • this to give me an error on hole regrex: "https?:(?:www)\.youtu(?:be\.com|\.be)embed[a-zA-Z0-9\-]*"; i chech this regrex on online regex101.com then it improved the regrex by this "https?:(?:www)\.youtu(?:be\.com|\.be)embed[a-zA-Z0-9\-]*"; but then it is giving that "Your regular expression does not match the subject string." and i pasted the improved regrex into my code then also it gives the error on hole regrex only.. – Shalini Upadhyay May 18 '17 at 08:46
  • Firstly, try to use backtics (\`) for inline code on stackoverflow(makes it more readable), Secondly, try `https?://(?:www)\.youtu(?:be\.com|\.be)/embed/[a-zA-Z0-9\-]*` and keep in mind that `matches` requires the entire string to match the pattern, and a url like `https://www.youtube.com/embed/M7lc1UVf-VE?autoplay=1&origin=http://example.com` would only match until the `?`, making matches say that there is no match (the part before the `?` is the minimal needed to make a valid embed link) – Jelmergu May 18 '17 at 09:26

1 Answers1

1
private String getYouTubeUrl(String text)
    {
        String finalUrl = null;
        String p = "(//www.youtube(?:-nocookie)?.com/(?:v|embed)/([a-zA-Z0-9-_]+).*)";

        if(text.contains("src"))
        {
            if(text.contains("//") && text.contains("frameborder"))
            {
                int startpos = text.indexOf("/", text.indexOf("src="));
                int endpos = text.indexOf("frameborder");
                String url=text.substring(startpos, endpos-2);

                if(url.matches(p))
                {
                    finalUrl = url;
                }
                else
                {
                    Window.alert("You have entered a wrong embed code");
                }   
            }
            else
            {
                Window.alert("You have entered a wrong embed code");
            }   
        }
        else
        {
            Window.alert("You have entered a wrong embed code");
        }
        return finalUrl;
    }
Vartika
  • 1,085
  • 3
  • 17
  • 43