RegEx用于匹配mp3 URL

时间:2019-05-17 23:34:14

标签: java regex regex-lookarounds regex-group regex-greedy

如何通过REGEX获取mp3网址?

此mp3网址,例如:

https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3

这是我到目前为止尝试过的方法,但我希望它仅接受结尾处带有“ .mp3”的网址。

(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]

3 个答案:

答案 0 :(得分:4)

此表达式可能会传递您想要的输入:

^(https?|ftp|file):\/\/(www.)?(.*?)\.(mp3)$

如果您想为其添加更多边界,则可以这样做。例如,您可以添加一个字符列表而不是.*

enter image description here

我添加了几个捕获组,只是为了易于调用(如有必要)。

RegEx

如果这不是您想要的表达式,则可以在regex101.com中修改/更改表达式。

RegEx电路

您还可以在jex.im中可视化您的表达式:

enter image description here

const regex = /^(https?|ftp|file):\/\/(www.)?(.*?)\.(mp3)$/gm;
const str = `https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3
http://soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3
http://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3
ftp://soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3
file://localhost/examples/mp3/SoundHelix-Song-1.mp3
file://localhost/examples/mp3/SoundHelix-Song-1.wav
file://localhost/examples/mp3/SoundHelix-Song-1.avi
file://localhost/examples/mp3/SoundHelix-Song-1.m4a`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}

Java测试

import java.util.regex.Matcher;
import java.util.regex.Pattern;

final String regex = "^(https?|ftp|file):\\/\\/(www.)?(.*?)\\.(mp3)$";
final String string = "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3\n"
     + "http://soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3\n"
     + "http://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3\n"
     + "ftp://soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3\n"
     + "file://localhost/examples/mp3/SoundHelix-Song-1.mp3\n"
     + "file://localhost/examples/mp3/SoundHelix-Song-1.wav\n"
     + "file://localhost/examples/mp3/SoundHelix-Song-1.avi\n"
     + "file://localhost/examples/mp3/SoundHelix-Song-1.m4a";

final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);

while (matcher.find()) {
    System.out.println("Full match: " + matcher.group(0));
    for (int i = 1; i <= matcher.groupCount(); i++) {
        System.out.println("Group " + i + ": " + matcher.group(i));
    }
}

答案 1 :(得分:2)

如果您希望它匹配以'.mp3'结尾的输入,则应在正则表达式的末尾添加\.mp3$

$表示表达式的结尾

(https?|ftp|file):\/\/[-a-zA-Z0-9+&@#\/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#\/%=~_|]\.mp3$

匹配:

https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3 **=> Match** 
https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp4 **=> No Match** 

答案 2 :(得分:0)

您可以使用锚点来声明字符串的开头^和结尾$,并使用.mp3结束模式:

^https?://\S+\.mp3$

说明

  • ^断言字符串的开头
  • https?://将http与可选s和://
  • 匹配
  • \S+匹配1次以上非空格字符
  • \.mp3匹配.mp3
  • $声明字符串结尾

Regex demo | Java demo

例如:

String regex = "^https?://\\S+\\.mp3$";
String[] strings = {
        "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3",
        "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp4"
};
Pattern pattern = Pattern.compile(regex);
for (String s : strings) {
    Matcher matcher = pattern.matcher(s);
    if (matcher.find()) {
        System.out.println(matcher.group(0));
    }
}

结果

https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3