java正则判断是否为链接
以下是一个Java正则表达式,用于检查字符串是否为链接:
String regex = "^(http|https)://[a-zA-Z0-9./?_&=-]*$";
这个正则表达式的解释如下:
^表示匹配字符串的开始位置(http|https)表示匹配以http或https开头的字符串://表示匹配:和//字符[a-zA-Z0-9./?_&=-]*表示匹配零个或多个字母、数字、斜杠、问号、下划线、连字符和等号$表示匹配字符串的结束位置
如果要使用这个正则表达式检查一个字符串是否为链接,可以使用Java的Pattern和Matcher类:
String str = "https://www.example.com/path/to/file.html";
Pattern pattern = Pattern.compile("^(http|https)://[a-zA-Z0-9./?_&=-]*$");
Matcher matcher = pattern.matcher(str);
if (matcher.matches()) {
System.out.println("是链接");
} else {
System.out.println("不是链接");
}
这段代码将输出 是链接,因为字符串 https://www.example.com/path/to/file.html 符合链接的格式
原文地址: https://www.cveoy.top/t/topic/gs20 著作权归作者所有。请勿转载和采集!