如何将所有youtube视频URL转换为java中的字符串代码?

如何将所有youtube视频URL转换为java中的字符串代码?,java,regex,Java,Regex,我有一个包含youtube URL的文本。我想将所有youtube URL转换为嵌入代码。 例如: 从 样品 样品 到 第二种方法 我试图通过在循环中查找所有youtube URL并将嵌入代码放置在子字符串方法中来放置嵌入代码 “\n++++\n+++++allowfullscreen> > ++++ > >“frameborder=“0”allow=“自动播放;加密媒体” >allowfullscreen> > ++++ 您可以使用Java的replaceAll()方法。 它还使用M

我有一个包含youtube URL的文本。我想将所有youtube URL转换为嵌入代码。 例如:

  • 样品

  • 样品

第二种方法

我试图通过在循环中查找所有youtube URL并将嵌入代码放置在子字符串方法中来放置嵌入代码

“\n++++\n+++++allowfullscreen>
> ++++
> 
>“frameborder=“0”allow=“自动播放;加密媒体”
>allowfullscreen>
> ++++

您可以使用Java的
replaceAll()
方法。 它还使用
Matcher()

一个简单的解决方案可以是这样的:

String url = "https://www.youtube.com/watch?v=xcJtL7QggTI?start=60";

public String getEmbedString(String url){
  String embed = "iframe width="560" height="315" src="@" frameborder="0"allow="autoplay; encrypted-media" allowfullscreen></iframe>";
  embed.replaceAll("@", url);

  return embed;
}
stringurl=”https://www.youtube.com/watch?v=xcJtL7QggTI?start=60";
公共字符串getEmbedString(字符串url){
String embed=“iframe width=“560”height=“315”src=“@”frameborder=“0”allow=“自动播放;加密媒体”allowfullscreen>”;
embed.replaceAll(“@”,url);
返回嵌入;
}

我还没有尝试使我的解决方案适应您的代码,因为这似乎简单得多。请告诉我这是否是您想要的。

问题在于我没有预料到的正则表达式模式。我进行了搜索,尝试了所有正则表达式模式,并发现了以下内容

此正则表达式模式成功查找所有URL。

Pattern MY\u Pattern=Pattern.compile(((http(s)?:\\\/\\\/)(www\\)?((youtube\\.com\\/)(youtu.be\\\/)[\\s]+);
Matcher m=我的模式。Matcher(内容);
StringBuffer sb=新的StringBuffer();
while(m.find()){
m、 附录替换(sb,“+++++\n”+“\n+++”;
}
m、 (某人);
Content=sb.toString();
Content=Content.replace(“youtu.be”、“youtube.com/embed”);
Content=Content.replace(“?t=”,“?start=”);

文本是指文本文件吗?是的。我阅读了文本文件和传输注释字符串的所有内容。我不知道您为什么在这里使用正则表达式。看起来您只是想替换
*Sample
后面的行,不管它是什么,所以不需要正则表达式,至少基于您发布的内容。在这种情况下,只需在所有行上循环,检查它是否为
*Sample
,如果是,则将一些
布尔值设置为true,如
afterSample
。如果
afterSample
为真,则读取该行,但写入其替换版本(被
包围着。我有不同的youtube URL,每个URL的id都不同…watch?v=xcJtL7QggTI…watch?v=thTTqefls有没有不使用regex的简单方法?但是从我看到的情况来看,你没有修改这些URL。那么,如果你知道这些URL在字符串中的位置,那么regex在这里有什么意义呢(样本
*后的下一行)?此代码将所有youtube URL转换为相同的URL。但我的文本中有不同的URL。我认为@IamSousakotiris给出了一个示例,说明了如何可以像您所问的那样轻松地将URL转换为嵌入式URL。他没有说如何从文本文件中检索每个URL。您可以根据您的解决方案调整他/她的答案是的。我是asking用于将URL转换为嵌入式URL,我在程序中完成了其他过程。该代码将所有不同的URL转换为此URL()。亲爱的朋友,您似乎有点困惑。上面的代码并没有声明
String url;
为最终版本。这意味着您应该这样做:
String url=您从您的文件中读取的任何url;
> "++++\n" + "<iframe width=\"560\" height=\"315\" src=\"" + How can I
> push URL here with following code?
> + "\" frameborder=\"0\" allow=\"autoplayencrypted-media\" allowfullscreen></iframe>\n++++"
Content = Content.replace("youtu.be", "youtube.com/embed");
Content = Content.replace("?t=", "?start=");
for (int i = -1; (i = Content.indexOf("https://youtu.be", i + 1)) != -1; i++) {
    Content = Content.substring(0, i) + "\n++++\n<iframe width=\"560\" height=\"315\" src=\"" + Content.substring(i, Content.length());
    Content = Content.substring(0, i + 78) + "\" frameborder=\"0\" allow=\"autoplay; encrypted-media\" allowfullscreen></iframe>\n++++\n" + Content.substring(i + 78, Content.length());
}
Content = Content.replace("youtu.be", "youtube.com/embed");
Content = Content.replace("?t=", "?start=");
> ++++ <iframe width="560" height="315" src="
> ++++ <iframe width="560" height="315" src="<iframe width="560" height="315" src="
> ++++ <iframe width="560" height="315" src=" https://youtube.com/watch?v=xcJtL7QggTI?t=60" frameborder="0"
> allow="autoplay; encrypted-media" allowfullscreen></iframe>
> ++++
> 
> " frameborder="0" allow="autoplay; encrypted-media"
> allowfullscreen></iframe>
> ++++
> 
> " frameborder="0" allow="autoplay; encrypted-media"
> allowfullscreen></iframe>
> ++++
String url = "https://www.youtube.com/watch?v=xcJtL7QggTI?start=60";

public String getEmbedString(String url){
  String embed = "iframe width="560" height="315" src="@" frameborder="0"allow="autoplay; encrypted-media" allowfullscreen></iframe>";
  embed.replaceAll("@", url);

  return embed;
}
Pattern MY_PATTERN = Pattern.compile("((http(s)?:\\/\\/)?)(www\\.)?((youtube\\.com\\/)|(youtu.be\\/))[\\S]+");
Matcher m = MY_PATTERN.matcher(Content);
StringBuffer sb = new StringBuffer();
while (m.find()) {
    m.appendReplacement(sb, "++++\n" + "<iframe width=\"560\" height=\"315\" src=\"" + m.group(0) + "\" frameborder=\"0\" allow=\"autoplayencrypted-media\" allowfullscreen></iframe>\n++++");
}
m.appendTail(sb);
Content = sb.toString();
Content = Content.replace("youtu.be", "youtube.com/embed");
Content = Content.replace("?t=", "?start=");