如何仅使用正则表达式验证嵌入的 YouTube link

How to validate the YouTube embedded link only using regular expression

我在我的网站中使用嵌入的 YouTube link。我想验证 link 就像用户粘贴其他东西然后嵌入 link 那么它应该给我一个警报无效 URL。我已经使用了很多正则表达式,有些已经在我的代码中了,我已经对它进行了评论。我只想嵌入 YouTube link 的正则表达式。这是我的代码:

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/' 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?=?([^#\&\?]*)

}
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;
    }