从短信中提取 URL

Extract URLs from SMS

我想要实现的是提取短信中包含的所有URL。当我说全部时,我指的是所有可点击的文本,即 SMS 中带下划线的文本。这是我正在尝试的代码并且它有效,但只有当 URL 以 http/https/ftp 开头时......我还需要得到 URL 没有那个。

public static List<String> extractUrls(String sms) {
    List<String> containedUrls = new ArrayList<String>();
    String text = sms;

    // Split the sms to analyze if each part is a URL
    String[] split = text.split(" ");

    // Attempt to convert each item into an URL
    for (int i = 0; i < split.length; i++) {
        if (URLUtil.isValidUrl(split[i])) containedUrls.add(split[i]);
    }

    return containedUrls;
}

您可以尝试使用正则表达式

public static List<String> extractUrls(String sms) {
List<String> containedUrls = new ArrayList<String>();
String text = sms;

// Split the sms to analyze if each part is a URL
String[] split = text.split(" ");

Pattern p = Pattern.compile("(@)?(href=')?(HREF=')?(HREF=\")?(href=\")?(http://)?[a-zA-Z_0-9\-]+(\.\w[a-zA-Z_0-9\-]+)+(/[#&\n\-=?\+\%/\.\w]+)?");

// Attempt to convert each item into an URL
for (int i = 0; i < split.length; i++) {
    if (p.matcher(split[i]).matches()) containedUrls.add(split[i]);
}

return containedUrls;
}