You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
21 lines
787 B
21 lines
787 B
package com.example.datacollect;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
// 工具类:专门验证URL格式
|
|
public class UrlValidator {
|
|
// 1. 定义URL正则表达式(简化版,能匹配http/https开头的网址)
|
|
private static final String URL_REGEX = "^(https?://)?([a-zA-Z0-9_-]+\\.)+[a-zA-Z]{2,6}(/.*)?$";
|
|
// 2. 编译正则表达式(提升匹配效率)
|
|
private static final Pattern URL_PATTERN = Pattern.compile(URL_REGEX);
|
|
|
|
// 3. 公开方法:验证URL是否合法,返回true/false
|
|
public static boolean isValidUrl(String url) {
|
|
// 先判断URL是否为空
|
|
if (url == null || url.trim().isEmpty()) {
|
|
return false;
|
|
}
|
|
// 用正则匹配URL
|
|
return URL_PATTERN.matcher(url.trim()).matches();
|
|
}
|
|
}
|