:!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
我想判断String str中是否包含上诉特殊字符,用正则怎么判断呢? Pattern p = Pattern.compile(" ");
 Matcher m = p.matcher("!");
 boolean b = m.matches();
System.out.println( b); 看材料看了很久,也试了很多,就是不能成功,高手指教

解决方案 »

  1.   

     String s = "~~!###我w@";
            
            Pattern p = Pattern.compile("\\W*"); //这里w为大写的,表示所有非法字符包括中文,如果不检测中文,得把中文的加进去,由于我搞忘了那个范围,所以就没写。中文的字符范围是一个:[0X1312-0X2342],具体是多少我忘了,楼下的来补充
            Matcher m = p.matcher(s);
            System.out.println(m.matches());
            //while(m.find()){
            //    System.out.println(m.group());打印找到的元素
            //}
      

  2.   

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;public class RegTest {
    public static void main(String[] args) {
    String specialStr = "[^\\:\\!\"\\#\\$\\%\\&\\'\\(\\)\\*\\+\\,\\-\\.\\/\\:\\;\\<\\=\\>\\?\\@\\[\\\\\\]\\^\\_\\`\\{\\|\\}\\~]*";
    Pattern pattern = Pattern.compile(specialStr);
    System.out.println(pattern.pattern());
    Matcher matcher = pattern.matcher("dfa{");
    System.out.println(matcher.matches());
    }
    }要进行转义: }转义写为\\}  \转义写为\\\\ 其他同
      

  3.   

    5楼 你的方法太那个了。
    直接specialStr = "\W*";
    就OK了
      

  4.   

    \W匹配一切非法字符
    比如,./;'[]<>?:"{}`~!@#$%^&*()_+|\=-;‘“。,?!space tab ....