日期:2014-05-20  浏览次数:20817 次

字符串过滤正则表达式相关问题
验证一个字符串,5-10位,英文或数字,只能有一位特殊字符,如果不符合条件抛出对应出错信息,求代码

------解决方案--------------------

public static void main(String[] args) throws IOException {
String reg = "(^[A-Za-z0-9]{5,10}$)
------解决方案--------------------
(^[A-Za-z0-9]{4,9}(&
------解决方案--------------------
#
------解决方案--------------------
%)$)";
System.out.println("dddd11#".matches(reg));
}

&
------解决方案--------------------
#
------解决方案--------------------
%可以随意增加特殊字符,记得用
------解决方案--------------------
分开

------解决方案--------------------
引用:
public static void verifyPassword(String str) throws Exception{
String regex = "\\w*[\\W]{0,1}\\w*";
Matcher m = Pattern.compile(regex,Pattern.CASE_INSENSITIVE).matcher(str);

if(str……
因为你用m.find()匹配"#3d$eg"的时候会得到"#3d","$eg"这样的匹配结果,而用str.matchers匹配是拿整个字符串进行匹配,你可以看看matcher.find()和 matcher.matches()的区别