正则表达式的验证方法

正则表达式的验证:

1.使用Pattern预编译字符串

2.调用Matcher的对象

3.使用该Matcher对象的matches方法来返回boolean类型的值(符合为true,不符合为false)

                String str = "123abcd";

		String exg= "^//d$";//判断是否符合数字类型

		Pattern p = Pattern.compile(exg);//完成预编译正则

		Matcher m = p.matcher(str );//进行匹配字符串

		boolean matches = m.matches();//返回匹配的结果
		
		
                //简写为如下
		boolean m1 = Pattern.compile(str).matcher(a).matches();

  

猜你喜欢

转载自www.cnblogs.com/charlypage/p/8975431.html