【问题标题】:RegEx for matching mp3 URLs用于匹配 mp3 URL 的正则表达式
【发布时间】:2019-10-05 06:40:12
【问题描述】:

如何使用 REGEX 获取 mp3 网址?

这个mp3 url,例如:

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

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

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

【问题讨论】:

  • 引用代码时请使用反引号(`
  • 那个正则表达式是正确的我认为你的应用或检查方法有问题,给我们提供更多的代码
  • 如果你希望它匹配以“.mp3”结尾的输入,你应该在你的正则表达式末尾添加\.mp3$

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


【解决方案1】:

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

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

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

我添加了几个捕获组,只是为了方便调用,如有必要。

正则表达式

如果这不是您想要的表达方式,您可以在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));
    }
}

【讨论】:

    【解决方案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** 
    

    【讨论】:

      【解决方案3】:

      您可以使用锚点来声明字符串的开头^ 和结尾$,并以.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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-10
        相关资源
        最近更新 更多