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

时间:2017-01-22 20:26:22

标签: java regex

我想使用Regex获取此String的所有.mp4网址。

另外,我想知道如何使用Regex只获取最后一个.mp4网址。

谢谢

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

2 个答案:

答案 0 :(得分:1)

<强>正则表达式:

$(document).on('keydown', 'input', function(e) {
    // keycode first
    if (e.keyCode==40 || e.keyCode==13) {
    }

    // keycode second etc.
    if (e.keyCode==39) {
    }

    if (e.keyCode==38) {
    }

    if (e.keyCode==37) {
    }
});

文字https?.*?\.mp4

随后是可选的&#39;:http

如果他们都使用HTTPS,请删除问号。

跟随尽可能少的字符:s?

后跟mp4扩展名(字面点).*?

答案 1 :(得分:0)

2方法:

  1. 如果您确定网址始终以https://开头,并且在完整网址完成后不会包含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. 如果没有,支持所有类型的网址,然后稍加修改,将模式更改为定义here

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

    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