java正则表达式

flybridy 2017-12-01 02:58:56
想写一个密码至少包含大写字母,小写字母,数字,且不少于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}$。。我加了/转义还是不行。
...全文
193 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

10,606

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 其他
社区管理员
  • 其他
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧