字符串过滤正则表达式相关问题

hty_1984 2013-04-01 11:01:59
验证一个字符串,5-10位,英文或数字,只能有一位特殊字符,如果不符合条件抛出对应出错信息,求代码
...全文
214 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
麻婆斗腐 2013-04-07
  • 打赏
  • 举报
回复
引用 12 楼 hty_1984 的回复:
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()的区别
hty_1984 2013-04-07
  • 打赏
  • 举报
回复
谁给回答一下12楼的问题,谢谢!
hty_1984 2013-04-02
  • 打赏
  • 举报
回复
貌似我的正则表达式还是有点问题,谁能基于我的代码把正则表达式那块改一下,就是验证一个字符串里只能有一个特殊字符,特殊字符的意思就是除了数字和字母之外的都是特殊字符,谢谢!
hty_1984 2013-04-02
  • 打赏
  • 举报
回复
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 == null | str.equals("")){ throw new Exception("The password is null."); } else if(str.length()<5){ throw new Exception("The password length is less than 5."); }else if(str.length()>10){ throw new Exception("The password length is more than 10."); //}else if(!str.matches(regex)){ }else if(!(m.find())){ throw new Exception("The password has more than 1 special character."); }else{ System.out.println("The password is OK."); } } public static void main(String[] args) throws Exception { verifyPassword("1234567009"); verifyPassword("ABcwz"); verifyPassword("3ABcw*13z"); verifyPassword("#3d$eg"); } 提交一下我的代码,但是verifyPassword("#3d$eg");验证的时候有问题,应该抛异常,但是提示“The password is OK.”,如果是改成注释的部分}else if(!str.matches(regex)){,就正常了,我想知道为什么?是我正则表达式的问题还是str.matches(regex)和m.find()这两种方式有什么差异?求解!
失落夏天 2013-04-01
  • 打赏
  • 举报
回复
特殊字符可以用阿斯科码来判断。。 是否具有一个可以根据[码的范围]{1}来判断, 但是前后都可以有普通字符,而且一共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));
	}

&|#|%可以随意增加特殊字符,记得用|分开
麻婆斗腐 2013-04-01
  • 打赏
  • 举报
回复
1.判断字符串长度 2.匹配英文和数字,计算英文和数字个数 3.字符串长度-英文数字数=特殊字符数 4.特殊字符数是否等于1,不等于1抛错
lvzg_005 2013-04-01
  • 打赏
  • 举报
回复
说的有点模糊,特殊字符指什么
幽狼 2013-04-01
  • 打赏
  • 举报
回复
^(?=[a-z\d]*?[^\da-z][a-z\d]*$)(?:[a-z\d]|[^a-z\d]){5,10}$
tzg157 2013-04-01
  • 打赏
  • 举报
回复

public static void main(String[] args) {
		String s = "aBd1#";
		Matcher m = Pattern.compile("&|#|%").matcher(s);
		System.out.println(!s.matches("_")?s.matches("[&#%\\w]{5,10}")?m.find()?!m.find():true:false:false);
	}
  • 打赏
  • 举报
回复

public static void main(String[] args) throws IOException {
		String reg = "[A-Za-z0-9]*[&|$|#]{0,1}[A-Za-z0-9]*";
		if (5 <= reg.length() || reg.length() <= 10) {
			System.out.println("#aaaa1a".matches(reg));
		}else{
			System.out.println("false");
		}

	}

看了下,挺复杂的,两部到位。凑合用……
hty_1984 2013-04-01
  • 打赏
  • 举报
回复
5-10位英文或数字,只有一位特殊字符(任意位置),谁给贴个能用的代码或正则表达式
hty_1984 2013-04-01
  • 打赏
  • 举报
回复
3楼的不行啊,特殊字符只能在最后一位才能通过,需求是特殊字符可以在任意位置,且只能有一位特殊字符,你那个把#换到别的位置就不行了呀
  • 打赏
  • 举报
回复
没看到3楼的方法吗?
hty_1984 2013-04-01
  • 打赏
  • 举报
回复
只有一位特殊字符用正则表达式是怎么写?

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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