如果我有以下网址:

http://www.example.com/wordpress/plugins/wordpressplugin/123/ver=1.0

如何获取插件的名称(在URL中简称为wordpressplugin)和版本,以便输出为-wordpressplugin 1.0版?

最佳答案

您可以使用Java中的Regex支持来做到这一点。

String url = "http://www.example.com/wordpress/plugins/wordpressplugin/123/ver=1.0";
Pattern pattern = Pattern.compile("(.*plugins/)(.*)(/\\d{3}/)(ver.*)");
Matcher matcher = pattern.matcher(url);
if (matcher.matches()) {
    System.out.println("Plugin: " + matcher.group(2));
    System.out.println("Version: " + matcher.group(4));
}

注意捕获组的使用。这是输出。
Plugin: wordpressplugin
Version: ver=1.0

07-28 02:32
查看更多