TestRegex

package com.njwb18081.day13;

public class TestRegex {
    /**
     * @param args
     */
    /**
     * @param args
     */
    public static void main(String[] args) {
        String str="aabbccddeeff";
        boolean b=str.matches("aabbccddeeff");//matches(String/regex格式)
        System.out.println(b);

        String regex="[abcdef]";//只包含a,b,c,d,e,f單個字符的正則
        System.out.println("z".matches(regex));//false
        System.out.println("f".matches(regex));//true




//      除了abc字符的正則
        System.out.println("f".matches("[^abc]"));//true
        System.out.println("a".matches("[^abc]"));//false


//      大寫A~D G~Z的正則
        System.out.println("E".matches("[A-D[G-Z]]"));//false
        System.out.println("H".matches("[A-D[G-Z]]"));//true






//      數字出現0次或一次
        System.out.println("66".matches("[0-9]?"));//false
//      數字出現0次或多次
        System.out.println("66".matches("[0-9]*"));//true
//      大寫A~Z英文字符最少4個最多6個
        System.out.println("AAAAAAA".matches("[A-Z]{4,6}"));

//      java中針對於字符串反斜槓\通過\做轉移
//      數字恰好是5次後跟小寫英文字母恰好是5次
        System.out.println("12321adsad".matches("(\\d){5}([a-z]{5})"));

//      QQ號 6位~11位 \d{6,11}
        System.out.println("1234567890".matches("\\d{6,11}"));

//      QQ郵箱 數字6~11位@qq.com
        System.out.println("[email protected]".matches("\\d{6,11}@qq.com"));

//      郵箱格式    數字6~11位@qq或sina.com或cn \\d{6,11}@(qq|sina).(com|cn)
//      1234567890@qq.com
//      1234567890@sina.com
//      1234567890@qq.cn
//      1234567890@sina.cn
        System.out.println("[email protected]".matches("\\d{6,11}@(qq|sina).(com|cn)"));

//      電話號碼    3~4位的數字-8位數字   \\d{3,4}-\d{8}
        System.out.println("0536-12345678".matches("\\d{3,4}-\\d{8}"));

//      密碼 首字母大寫後跟8~11位英文或數字@qq或sina.com或cn
//      [A-Z]{1}(\\d|[a-zA-Z]) {8,11}@(qq|sina).(com|cn)
        System.out.println("[email protected]".matches("[A-Z]{1}(\\d|[a-zA-Z]){8,11}@(qq|sina).(com|cn)"));

        String str2="sadsa12321sadsad213213sadsad32132";
        System.out.println(str2.replaceAll("\\d{5,6}", "HELLO"));

        str2="13955663344,13955664544,13955668844,13955665644";
        System.out.println(str2.replaceAll("(\\d{3})(\\d{4})(\\d{4})", "$1****$3"));



    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章