最近使用JAVA正则表达式,有个匹配URL的例子很实用,贴出来分享一下!
文件名称:Test.java
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test{ public static void main(String[] args){ System.out.println(isUrl("http://www.tmall.com/sss.html")); System.out.println(isUrl("http://www.baidu.com/sss.html")); } /** * URL检查<br> * <br> * @param pInput 要检查的字符串<br> * @return boolean 返回检查结果<br> */ public static boolean isUrl (String pInput) { if(pInput == null){ return false; } String regEx = "^(https?|ftp|file)://[a-zA-Z0-9-_]*.tmall.com/[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]"; Pattern p = Pattern.compile(regEx); Matcher matcher = p.matcher(pInput); return matcher.matches(); } }
运行结果:
@~/study $ java Test
true false