用于匹配 mp3 URL 的正则表达式

RegEx for matching mp3 URLs

如何使用 REGEX 获得 mp3 url?

这个mp3url,例如:

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

这是我迄今为止尝试过的方法,但我希望它只接受结尾带有“.mp3”的 url。

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

如果您希望它匹配以“.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** 

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

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

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

我添加了几个捕获组,只是为了方便调用,如果需要的话。

正则表达式

如果这不是您想要的表达方式,您可以 modify/change 您的表达方式 regex101.com

正则表达式电路

您还可以在 jex.im:

中可视化您的表情

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));
    }
}

您可以使用锚点断言字符串的开始 ^ 和结束 $ 并以 .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