项目场景:
使用正则匹配出字符串中英文字母和数字(其它标识可直接替换)
解决方案:
//匹配出字符中不包含数字和字母的字符串 `public static final String REGEX_CHAR = “[^(a-zA-Z0-9)]”; public static final String REGEX_CHAR2 = “[(a-zA-Z0-9)]+”; public static void main(String[] args) { String context=“abc!@#$%^&*33#@,.ab90-2=”; //方案一:匹配出不达要求的字母和数字 并用空出串替换 String noneChinaWords = context.replaceAll(REGEX_CHAR, “”); System.out.println(noneChinaWords);
//方案二:正则匹配出符合要求的字母和数字 Pattern pwdPattern = Pattern.compile(REGEX_CHAR2); Matcher matcher = pwdPattern.matcher(context); 通过matcher 匹配 find查找对应字符元素 while (matcher.find()){ System.out.println(matcher.group()); }
精彩文章
发表评论