新的起点 新的开始

快乐生活 !

请正则表达式高手给出更好的解决方案

    在目前的项目中,对用户输入的密码的校验有如下要求:
               1. 必须8位到15位。
               2.至少包含如何四组中的三组。
                   (1)  包含a-z
                   (2)  包含A-Z
                   (3)  包含0-9
                   (4)   包含特殊字符:@#$%
    本人对正则表达式也是懵懵懂懂,只能像如下比较弱弱的实现,请正则表达式高手给出更好的解决方案,当然其他方式也欢迎!

    /*
     *  Validate password  follow setups:
     *  1. if parameter is null return false;
     *  2. if parameter's length is not from 8 to 15 return false;
     *  3. if parameter include other character  return false;
     *  4. if parameter has group of CapitalCharacter or Lowercase or Number or SpecialCharacter 
     *     and more then three groups then return true whereas return false;
     * 
     
*/

    
public static boolean validatePassword(String str){
        
boolean isValidated = false;
        
if(str==null){return isValidated;}  //setup 1  
        
        
if(str.length()<8 || str.length()>15){return isValidated;} //setup 2  
        
        String includeOthers
="[a-z|A-Z|0-9|@#$%]+";     //setup 3 
        Pattern p = Pattern.compile(includeOthers);
        Matcher m 
= p.matcher(str);
        
if(!m.matches()){
            
return isValidated; // include other invalid character, return false;
        }

        String validate
="([0-9]+)|([a-z]+)|([A-Z]+)|([@#$%]+)"//setup 4
        p = Pattern.compile(validate);
        m 
= p.matcher(str);
        
boolean hasCapitalCharacter = false;
        
boolean hasLowercase  = false;
        
boolean hasNumber  = false;
        
boolean hasSpecialCharacter  = false;
        
while (m.find()) {
            
if(m.group(1)!=null &&!"".equals(m.group(1))) hasNumber = true;
            
if(m.group(2)!=null &&!"".equals(m.group(2))) hasLowercase = true;
            
if(m.group(3)!=null &&!"".equals(m.group(3))) hasCapitalCharacter = true;
            
if(m.group(4)!=null &&!"".equals(m.group(4))) hasSpecialCharacter = true;
        }

        
int count = 0;
        
if(hasCapitalCharacter)count++;
        
if(hasLowercase)count++;
        
if(hasNumber)count++;
        
if(hasSpecialCharacter)count++;
        
if(count>=3)isValidated =true;
        
return isValidated;
        
    }


静静等候中..........................

posted on 2009-07-22 22:16 advincenting 阅读(1446) 评论(11)  编辑  收藏

评论

# re: 请正则表达式高手给出更好的解决方案 2009-07-22 23:53 菜菜宝宝

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PasswordTest {

private final static Matcher passwordMatcher = Pattern.compile(
"^(?:([a-z])|([A-Z])|([0-9])|([@#$%])){8,15}$"
).matcher("");

public static void main(String[] args) {

String[] strs = {
"abcdefg12345",
"aaabbbAAA$$$",
"aaabbbAAAa@13434",
"aaAA11",
"AAAaaa113@"
};

for(int i = 0; i < strs.length; i++) {
System.out.printf("str: %-20s length: %2d result: %s%n",
strs[i],
strs[i].length(),
checkPassword(strs[i])
);
}
}

public static boolean checkPassword(String password) {

if (password == null) {
return false;
}

passwordMatcher.reset(password);

if (!passwordMatcher.matches()) {
return false;
}

int count = 0;
for (int i = passwordMatcher.groupCount(); i > 0; i--) {
if (passwordMatcher.start(i) > -1) {
count++;
}
}
return (count >= 3);
}
}

  回复  更多评论   

# re: 请正则表达式高手给出更好的解决方案 2009-07-22 23:55 游客

长度Check:
[a-zA-Z0-9@#$%]{8,15}
有效性Check:
([a-z]+[A-Z]+[0-9]+)|
([A-Z]+[0-9]+[@#$%]+)|
([a-z]+[0-9]+[@#$%]+)|
([a-z]+[A-Z]+[@#$%]+)

使用前请好好测试一下儿!  回复  更多评论   

# re: 请正则表达式高手给出更好的解决方案 2009-07-23 00:01 游客

@菜菜宝宝

aaaZXXXXCCCC

好像这个字符串也可以被匹配上吧!
这和LZ的要求不太符合!  回复  更多评论   

# re: 请正则表达式高手给出更好的解决方案 2009-07-23 00:03 菜菜宝宝

@游客

您好,aaaZXXXXCCCC 这个的匹配结果返回的是 false 啊。  回复  更多评论   

# re: 请正则表达式高手给出更好的解决方案 2009-07-23 09:56 advincenting

菜菜宝宝 不错!  回复  更多评论   

# re: 请正则表达式高手给出更好的解决方案[未登录] 2009-07-23 12:55 cxh

还在后台判断?为什么不在前台判断呢?  回复  更多评论   

# re: 请正则表达式高手给出更好的解决方案[未登录] 2009-07-23 17:13 john

@菜菜宝宝
赞,学习  回复  更多评论   

# re: 请正则表达式高手给出更好的解决方案 2009-07-23 21:55 xing.liu

^((?=.*\d)(?=.*[A-Z])(?=.*[a-z])|(?=.*\d)(?=.*[A-Z])(?=.*[@#$%])|(?=.*\d)(?=.*[a-z])(?=.*[@#$%])|(?=.*[A-Z])(?=.*[a-z])(?=.*[@#$%]))[0-9a-zA-Z@#$%]{8,15}$  回复  更多评论   

# re: 请正则表达式高手给出更好的解决方案 2009-07-23 21:57 xing.liu

当作是复习一下正则.  回复  更多评论   

# re: 请正则表达式高手给出更好的解决方案 2009-07-23 22:01 xing.liu

@菜菜宝宝
999999@LLL 不匹配  回复  更多评论   

# re: 请正则表达式高手给出更好的解决方案 2009-07-25 01:59 菜菜宝宝

@xing.liu

@菜菜宝宝
999999@LLL 不匹配
----------------------------------
999999@LLL 我测试的结果是 true 啊?符合博主的要求。  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 

公告

Locations of visitors to this page

导航

<2024年3月>
252627282912
3456789
10111213141516
17181920212223
24252627282930
31123456

统计

常用链接

留言簿(13)

随笔分类(71)

随笔档案(179)

文章档案(13)

新闻分类

IT人的英语学习网站

JAVA站点

优秀个人博客链接

官网学习站点

生活工作站点

最新随笔

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜