简体   繁体   English

用于在字符串中查找 mp4 的正则表达式

[英]Regex for finding mp4 in string

I want to get all .mp4 URLs of this String using Regex.我想使用正则表达式获取此字符串的所有 .mp4 URL。

Also I want to know how to get only the last .mp4 URL using Regex.另外我想知道如何使用正则表达式只获取最后一个 .mp4 URL。

Thanks谢谢

contentType=application/x-mpegURL, url=https://video.twimg.com/amplify_video/822938952332144642/pl/BjHU8aBCbOgZNzXQ.m3u8}, 

Variant{bitrate=0, contentType=application/dash+xml, url=https://video.twimg.com/amplify_video/822938952332144642/pl/BjHU8aBCbOgZNzXQ.mpd}, 

Variant{bitrate=320000, contentType=video/mp4, url=https://video.twimg.com/amplify_video/822938952332144642/vid/320x180/YqZ72rzLj3VWVhy4.mp4}, 

Variant{bitrate=832000, contentType=video/mp4, url=https://video.twimg.com/amplify_video/822938952332144642/vid/640x360/A2vMgzo2ElpPP6TE.mp4}, 

Variant{bitrate=2176000, contentType=video/mp4, url=https://video.twimg.com/amplify_video/822938952332144642/vid/1280x720/j9xbNzRZqEbYs_2s.mp4}]}]";

Regex:正则表达式:

https?.*?\.mp4

Literal http文字http

Followed by an optional 's': s?后跟一个可选的“s”: s?

Remove the question mark if they will all use HTTPS.如果他们都将使用 HTTPS,请删除问号。

Followed by as few characters as possible: .*?后跟尽可能少的字符: .*?

Followed by an mp4 extension (literal dot) \\.mp4后跟一个 mp4 扩展名(文字点) \\.mp4

2 Approaches: 2 方法:

  1. If you're sure the URL's will always begin with https:// and will not contain a mp4 after the complete URL is finished, then you can use pattern = "https://.*mp4";如果您确定 URL 将始终以https://开头并且在完整 URL 完成后不包含mp4 ,那么您可以使用pattern = "https://.*mp4";

     String[] arr = { "contentType=application/x-mpegURL, url=https://video.twimg.com/amplify_video/822938952332144642/pl/BjHU8aBCbOgZNzXQ.m3u8}", "Variant{bitrate=0, contentType=application/dash+xml, url=https://video.twimg.com/amplify_video/822938952332144642/pl/BjHU8aBCbOgZNzXQ.mpd}", "Variant{bitrate=320000, contentType=video/mp4, url=https://video.twimg.com/amplify_video/822938952332144642/vid/320x180/YqZ72rzLj3VWVhy4.mp4}", "Variant{bitrate=832000, contentType=video/mp4, url=https://video.twimg.com/amplify_video/822938952332144642/vid/640x360/A2vMgzo2ElpPP6TE.mp4}", "Variant{bitrate=2176000, contentType=video/mp4, url=https://video.twimg.com/amplify_video/822938952332144642/vid/1280x720/j9xbNzRZqEbYs_2s.mp4}]}]" }; String pattern = "https://.*mp4"; Pattern r = Pattern.compile(pattern); for (String line : arr) { Matcher m = r.matcher(line); if (m.find()) { System.out.println(m.group(0)); } else { System.out.println("NO MATCH"); } }
  2. If not, to Support all types of URL's then change your pattern to what is defined here with a little modification,如果没有,为了支持所有类型的 URL ,只需稍作修改即可将您的模式更改为此处定义的模式,

     String pattern = "(((ht|f)tp(s?)\\\\:\\\\/\\\\/|~\\\\/|\\\\/)|www.)" + "(\\\\w+:\\\\w+@)?(([-\\\\w]+\\\\.)+(com|org|net|gov" + "|mil|biz|info|mobi|name|aero|jobs|museum" + "|travel|[az]{2}))(:[\\\\d]{1,5})?" + "(((\\\\/([-\\\\w~!$+|.,=]|%[af\\\\d]{2})+)+|\\\\/)+|\\\\?|#)?" + "((\\\\?([-\\\\w~!$+|.,*:]|%[af\\\\d{2}])+=?" + "([-\\\\w~!$+|.,*:=]|%[af\\\\d]{2})*)" + "(&(?:[-\\\\w~!$+|.,*:]|%[af\\\\d{2}])+=?" + "([-\\\\w~!$+|.,*:=]|%[af\\\\d]{2})*)*)*" + "(#([-\\\\w~!$+|.,*:=]|%[af\\\\d]{2})*)?\\\\b"+"mp4";

Output:输出:

NO MATCH
NO MATCH
https://video.twimg.com/amplify_video/822938952332144642/vid/320x180/YqZ72rzLj3VWVhy4.mp4
https://video.twimg.com/amplify_video/822938952332144642/vid/640x360/A2vMgzo2ElpPP6TE.mp4
https://video.twimg.com/amplify_video/822938952332144642/vid/1280x720/j9xbNzRZqEbYs_2s.mp4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM