gpt4 book ai didi

java - 如何将所有 youtube 视频网址转换为将代码嵌入到 java 中的字符串中?

转载 作者:行者123 更新时间:2023-12-05 06:31:41 27 4
gpt4 key购买 nike

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

来自

* Sample
++++
<iframe width="560" height="315" src="https://www.youtube.com/embed/S-thTTqefls?start=60" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
++++


* Sample
++++
<iframe width="560" height="315" src="https://www.youtube.com/embed/xcJtL7QggTI?start=60" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
++++

第一种方法

我想在嵌入代码之间推送匹配的 youtube 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++++"

字符串内容 = readFileAsString(路径); 模式 MY_PATTERN = Pattern.compile("^(https?\:\/\/)?(www\.)?(youtube\.com|youtu\.?be)\/.+$"); 匹配器 m = MY_PATTERN.matcher(Content); Content = m.replaceAll("++++\n"+ "\n++++");

Content = Content.replace("youtu.be", "youtube.com/embed");
Content = Content.replace("?t=", "?start=");

第二种方法

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

"\n++++\n<iframe width=\"560\" height=\"315\" src=\"" 44个字符

https://youtu.be/S-thTTqefls?t=60最多 34 个字符

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=");

但是有些https://youtu.be URL 没有嵌入代码。但是我有这样一个奇怪的输出。

> ++++ <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>
> ++++

最佳答案

问题是我没想到的正则表达式模式。我进行了搜索并尝试了所有正则表达式模式并找到了以下内容

此正则表达式模式成功找到所有 URL。 https://stackoverflow.com/a/31726735/9134980

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=");

关于java - 如何将所有 youtube 视频网址转换为将代码嵌入到 java 中的字符串中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51685467/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com