gpt4 book ai didi

java - 使用或不使用 RegEx 提取 YouTube ID

转载 作者:搜寻专家 更新时间:2023-10-31 20:00:48 24 4
gpt4 key购买 nike

请告诉我如何在不使用正则表达式的情况下获取 youtube ID?

在 URL 之后使用上述方法,无效

http://www.youtube.com/e/dQw4w9WgXcQ

http://www.youtube.com/watch?feature=player_embedded&v=dQw4w9WgXcQ

public static String extractYTId(String youtubeUrl) {
String video_id = "";

try {
if(youtubeUrl != null && youtubeUrl.trim().length() > 0 && youtubeUrl.startsWith("http")) {
String expression = "^.*((youtu.be" + "\\/)" + "|(v\\/)|(\\/u\\/w\\/)|(embed\\/)|(watch\\?))\\??v?=?([^#\\&\\?]*).*"; // var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
//String expression = "^.*(?:youtu.be\\/|v\\/|e\\/|u\\/\\w+\\/|embed\\/|v=)([^#\\&\\?]*).*";
CharSequence input = youtubeUrl;
Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(input);
if(matcher.matches()) {
String groupIndex1 = matcher.group(7);
if(groupIndex1 != null && groupIndex1.length() == 11)
video_id = groupIndex1;
}
}
} catch(Exception e) {
Log.e("YoutubeActivity", "extractYTId " + e.getMessage());
}

return video_id;
}

其他链接有效正常

http://www.youtube.com/v/0zM3nApSvMg?fs=1&hl=en_US&rel=0

​​http://www.youtube.com/embed/0zM3nApSvMg?rel=0

http://www.youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index

http://www.youtube.com/watch?v=0zM3nApSvMg

http://youtu.be/0zM3nApSvMg

http://www.youtube.com/watch?v=0zM3nApSvMg#t=0m10s

http://youtu.be/dQw4w9WgXcQ

http://www.youtube.com/embed/dQw4w9WgXcQ

http://www.youtube.com/v/dQw4w9WgXcQ

http://www.youtube.com/watch?v=dQw4w9WgXcQ

​​​​http://www.youtube-nocookie.com/v/6L3ZvIMwZFM?version=3&hl=en_US&rel=0

最佳答案

您可以使用以下正则表达式

^(?:(?:https?:\/\/)?(?:www\.)?)?(youtube(?:-nocookie)?\.com|youtu\.be)\/.*?(?:embed|e|v|watch\?.*?v=)?\/?([a-z0-9]+)

正则表达式分解:

  1. ^:行首 anchor
  2. (?:(?:https?:\/\/)?(?:www\.)?)?:
    • (?:https?:\/\/)?:可选匹配http://https://<
    • (?:www\.)?)?:匹配www.零次或一次
  3. (youtube(?:-nocookie)?\.com|youtu\.be)\/:匹配
    • youtube.comyoutube-nocookie.comyoutu.be 后跟 /
  4. .*?:惰性匹配。匹配直到下一个模式满足。
  5. (?:embed|e|v|watch\?.*?v=)?\/?:
    • (?:embed|e|v|watch\?.*?v=)?:匹配embedev 或从 watch?v= 或nothing
    • \/?:匹配/零次或一次
  6. ([a-z0-9]+):匹配一个或多个字母数字字符并将其添加到捕获的组中。

现场演示使用 JavaScript

var regex = /^(?:(?:https?:\/\/)?(?:www\.)?)?(youtube(?:-nocookie)?\.com|youtu\.be)\/.*?(?:embed|e|v|watch\?.*?v=)?\/?([a-z0-9]+)/i;

// An array of all the youtube URLs
var youtubeLinks = [
'http://www.youtube.com/e/dQw4w9WgXcQ',
'http://www.youtube.com/watch?feature=player_embedded&v=dQw4w9WgXcQ',
'http://www.youtube.com/v/0zM3nApSvMg?fs=1&hl=en_US&rel=0',
'http://www.youtube.com/embed/0zM3nApSvMg?rel=0',
'http://www.youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index',
'http://www.youtube.com/watch?v=0zM3nApSvMg',
'http://youtu.be/0zM3nApSvMg',
'http://www.youtube.com/watch?v=0zM3nApSvMg#t=0m10s',
'http://youtu.be/dQw4w9WgXcQ',
'http://www.youtube.com/embed/dQw4w9WgXcQ',
'http://www.youtube.com/v/dQw4w9WgXcQ',
'http://www.youtube.com/watch?v=dQw4w9WgXcQ',
'http://www.youtube-nocookie.com/v/6L3ZvIMwZFM?version=3&hl=en_US&rel=0'
];

// An object to store the results
var youtubeIds = {};

// Iterate over the youtube URLs
youtubeLinks.forEach(function(url) {
// Get the value of second captured group to extract youtube ID
var id = "<span class='youtubeId'>" + (url.match(regex) || [0, 0, 'No ID present'])[2] + "</span>";

// Add the URL and the extracted ID in the result object
youtubeIds[url] = id;
});

// Log the object in the browser console
console.log(youtubeIds);

// To show the result on the page
document.getElementById('output').innerHTML = JSON.stringify(youtubeIds, 0, 4);
.youtubeId {
color: green;
font-weight: bold;
}
<pre id="output"></pre>

RegEx Visualization Diagram

关于java - 使用或不使用 RegEx 提取 YouTube ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35435555/

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