java正则表达式
想写一个密码至少包含大写字母,小写字母,数字,且不少于8位的正则表达式。在js中可以实现了。没问题。但是项目结构原因不方面在js中写。需要在后台验证,按时把这个reg传入进去没用。请问需要怎么写?
1.js中的写法,这个是OK的。
function test(){
//var text="index.aspx?test=1&ww=2&www=3"; //
var text= document.getElementById("test1").value;
var re =/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[^]{8,16}$/;
var result= re.test(text);
if(result)
{
alert("ok");
}else
{
alert("密码至少包含大写字母,小写字母,数字,且不少于8位");
}
}
2.java目前的写法,然后报错了。、
public class Test {
public static void main(String args[]){
String reg="^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)[^]{8,16}$";
System.out.println(startCheck(reg,"12313"));
}
public static boolean startCheck(String reg,String string)
{
boolean tem=false;
Pattern pattern = Pattern.compile(reg);
Matcher matcher=pattern.matcher(string);
tem=matcher.matches();
return tem;
}
}
报错内容说的是 Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character class near index 42
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[^]{8,16}$。。我加了/转义还是不行。