1. 程式人生 > >java正則學習筆記二

java正則學習筆記二

空白字符 vat 如果 system sys class regex ID mat

public class Demo3_Regex {

/**
 *  . 任何字符
    \d 數字:[0-9] 
    \D 非數字: [^0-9] 
    \s 空白字符:[ \t\n\x0B\f\r] 
    \S 非空白字符:[^\s] 
    \w 單詞字符:[a-zA-Z_0-9] 
    \W 非單詞字符:[^\w] 

 */
public static void main(String[] args) {
    //demo1();
    //demo2();
    //demo3();
    //demo4();
    //demo5();
    //demo6();
    String regex = "\\W";
    System.out.println("a".matches(regex));
    System.out.println("z".matches(regex));
    System.out.println("_".matches(regex));
    System.out.println("%".matches(regex));
}

private static void demo6() {
    String regex = "\\w";
    System.out.println("a".matches(regex));
    System.out.println("z".matches(regex));
    System.out.println("_".matches(regex));
    System.out.println("%".matches(regex));
}

private static void demo5() {
    String regex = "\\S";
    System.out.println(" ".matches(regex));
    System.out.println("    ".matches(regex));
    System.out.println("a".matches(regex));
}

private static void demo4() {
    String regex = "\\s";
    System.out.println(" ".matches(regex));
    System.out.println("    ".matches(regex));              //一個tab鍵
    System.out.println("    ".matches(regex));              //四個空格
}

private static void demo3() {
    String regex = "\\D";
    System.out.println("0".matches(regex));
    System.out.println("9".matches(regex));
    System.out.println("a".matches(regex));
}

private static void demo2() {
    String regex = "\\d";                   //\代表轉義字符,如果想表示\d的話,需要\\d
    System.out.println("0".matches(regex));
    System.out.println("a".matches(regex));
    System.out.println("9".matches(regex));
}

private static void demo1() {
    String regex = "..";
    System.out.println("a".matches(regex));
    System.out.println("ab".matches(regex));
}

}

java正則學習筆記二