Home > Java > JavaBase > How to determine whether a string is a url in java

How to determine whether a string is a url in java

王林
Release: 2019-11-21 14:05:52
Original
5817 people have browsed it

How to determine whether a string is a url in java

概述:

Pattern类的作用在于编译正则表达式后创建一个匹配模式。

Matcher类使用Pattern实例提供的模式信息对正则表达式进行匹配。

判断方法:

在用字符串String设置完一条正则表达式之后,通过Pattern.compile编译正则表达式创建一个匹配模式,之后再用pattern.matcher(xx).matches()的方法,得到是否匹配的布尔值。

示例如下:

package urlReg;
import java.util.regex.*;
public class urlRegTest {
	public static void main(String[] args) {
		String url1 = "http://www.xx.com";
		String url2 = "w.xx.com";
		String url3 = "http://w.xx.com";
		String url4 = "ssss";
		Pattern pattern = Pattern
				.compile("^([hH][tT]{2}[pP]://|[hH][tT]{2}[pP][sS]://)(([A-Za-z0-9-~]+).)+
				([A-Za-z0-9-~\\/])+$");
		System.out.println(pattern.matcher(url1).matches());
		System.out.println(pattern.matcher(url2).matches());
		System.out.println(pattern.matcher(url3).matches());
		System.out.println(pattern.matcher(url4).matches());
	}
}
Copy after login

推荐教程:java快速入门

The above is the detailed content of How to determine whether a string is a url in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template