猿问

检查字符串是否包含特殊字符

如何检查字符串是否包含特殊字符,例如:


[,],{,},{,),*,|,:,>,


白衣染霜花
浏览 950回答 3
3回答

繁星淼淼

如果您希望密码中包含字母,特殊字符和数字(至少8位数字),那么使用此代码,即可正常运行public static boolean Password_Validation(String password)&nbsp;{&nbsp; &nbsp; if(password.length()>=8)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Pattern letter = Pattern.compile("[a-zA-z]");&nbsp; &nbsp; &nbsp; &nbsp; Pattern digit = Pattern.compile("[0-9]");&nbsp; &nbsp; &nbsp; &nbsp; Pattern special = Pattern.compile ("[!@#$%&*()_+=|<>?{}\\[\\]~-]");&nbsp; &nbsp; &nbsp; &nbsp; //Pattern eight = Pattern.compile (".{8}");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Matcher hasLetter = letter.matcher(password);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Matcher hasDigit = digit.matcher(password);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Matcher hasSpecial = special.matcher(password);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return hasLetter.find() && hasDigit.find() && hasSpecial.find();&nbsp; &nbsp; }&nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; return false;}
随时随地看视频慕课网APP

相关分类

Java
我要回答