概述:
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()); } }
推荐教程: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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version
SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver CS6
Visual web development tools