最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java 中的正則表達(dá)式單字符預(yù)定義字符匹配問題

 更新時間:2022年11月29日 11:47:25   作者:JavaLearnerZGQ  
正則表達(dá)式用極簡的規(guī)則取代了復(fù)雜的驗證邏輯,是一種通用的技術(shù),適用于多種編程語言,近通過本文給大家講解Java 中的正則表達(dá)式單字符匹配和預(yù)定義字符匹配問題,感興趣的朋友跟隨小編一起看看吧

一、需求

? 現(xiàn)有一個字符串,需滿足如下要求:
[6, 18] 個字符
② 只能包含字母、數(shù)字、下劃線
③ 需以字母開頭
? 如果字符串滿足上述要求,返回 true,否則返回 false

public static boolean validString(String s) {
       return s.matches("[a-zA-Z][a-zA-Z0-9_]{5,17}");
   }

?? 正則表達(dá)式用極簡的規(guī)則取代了復(fù)雜的驗證邏輯
?? Regex Expression
?? 正則表達(dá)式是一種通用的技術(shù),適用于多種編程語言

二、單字符匹配(6個)

?? ① [abc]:字符串的某個位置(某一個字符)滿足 a、b、c 中的一個

?? 某個位置:該【單字符匹配】放的位置

public class TestDemo {
    public static void main(String[] args) {
        String regex = "[zgq]";
        System.out.println("z".matches(regex)); // true
        System.out.println("g".matches(regex)); // true
        System.out.println("q".matches(regex)); // true
        System.out.println("zgq".matches(regex)); // false
    }
}
public class TestDemo {
    public static void main(String[] args) {
        String regex = "26[abc]3q";
        System.out.println("26a3q".matches(regex)); // true
        System.out.println("26abc".matches(regex)); // false
        System.out.println("26b3q".matches(regex)); // true 
    }
}

?? ② [^abc]:除了 a、b、c 之外的任意單個字符

public class TestDemo {
    public static void main(String[] args) {
        String regex = "[^cat]666";
        System.out.println("c666".matches(regex)); // false
        System.out.println("a666".matches(regex)); // false
        System.out.println("t666".matches(regex)); // false
        System.out.println("bb666".matches(regex)); // false
        System.out.println("b666".matches(regex)); // true
    }
}
public class TestDemo {
    public static void main(String[] args) {
        String regex1 = "[12345]666";
        String regex2 = "[^1-5]666";
        System.out.println("1666".matches(regex1)); // true
        System.out.println("3666".matches(regex1)); // true
        System.out.println("5666".matches(regex1)); // true
        System.out.println("6666".matches(regex1)); // false

        System.out.println("1666".matches(regex2)); // false
        System.out.println("3666".matches(regex2)); // false
        System.out.println("5666".matches(regex2)); // false

        System.out.println("6666".matches(regex2)); // true
    }
}

?? ③ [a-zA-z]:匹配單個英文字母

public class TestDemo {
    public static void main(String[] args) {
        String regex = "[a-zA-Z]666";
        System.out.println("6666".matches(regex)); // false
        System.out.println("b666".matches(regex)); // true
    }
}

?? ④ [a-d[1-6]]:和 [a-d1-6] 一樣(并集)

public class TestDemo {
    public static void main(String[] args) {
        String regex1 = "[a-d[1-6]]";
        String regex2 = "[a-d1-6]";
        System.out.println("a".matches(regex1)); // true
        System.out.println("e".matches(regex1)); // false
        System.out.println("1".matches(regex1)); // true
        System.out.println("7".matches(regex1)); // false

        System.out.println("a".matches(regex2)); // true
        System.out.println("e".matches(regex2)); // false
        System.out.println("1".matches(regex2)); // true
        System.out.println("7".matches(regex2)); // false
    }
}

?? ⑤ [zgq&&[god]]:交集

public class TestDemo {
    public static void main(String[] args) {
        String regex1 = "[zgq&&[god]]";
        System.out.println("q".matches(regex1)); // false
        System.out.println("d".matches(regex1)); // false
        System.out.println("g".matches(regex1)); // true
    }
}

?? ⑥ [zgq&&[god]]:取差集

public class TestDemo {
    public static void main(String[] args) {
        String regex1 = "[zgq&&[^god]]";
        System.out.println("q".matches(regex1)); // true
        System.out.println("d".matches(regex1)); // false
        System.out.println("g".matches(regex1)); // false
        System.out.println("z".matches(regex1)); // true

        // 取差集, 從字母 a 到字母 z 中去除字母 b 和 d
        String regex2 = "[a-z&&[^bd]]";
        System.out.println("d".matches(regex2)); // false
        System.out.println("a".matches(regex2)); // true
    }
}

三、預(yù)定義字符(7個)

預(yù)定義字符匹配的仍然是單個字符

?? 【.】:任意單個字符
?? 【\d】:數(shù)字
?? 【\D】:非數(shù)字
?? 【\s】:空白
?? 【\S】:非空白
?? 【\w】:字母(英文字母、下劃線、數(shù)字)
?? 【\W】:非英文字母

?? Java 中需以兩個【\】開頭表示預(yù)定義字符

public class TestDemo {
    public static void main(String[] args) {
        String r1 = ".";
        System.out.println("@".matches(r1)); // true
        System.out.println("慶".matches(r1)); // true
        System.out.println("I".matches(r1)); // true
        System.out.println(" ".matches(r1)); // true
        System.out.println(".".matches(r1)); // true
    }
}
public class TestDemo {
    public static void main(String[] args) {
        // 匹配 java 文件
        String r1 = ".\\.java";
        System.out.println("a.java".matches(r1)); // true
        System.out.println("xjava".matches(r1)); // false
        System.out.println("5java".matches(r1)); // false
    }
}
public class TestDemo {
    public static void main(String[] args) {
        String r1 = "[abc]";
        String r2 = "\\[abc\\]";
        System.out.println("a".matches(r1)); // true
        System.out.println("c".matches(r1)); // true
        System.out.println("[abc]".matches(r1)); // false

        System.out.println("a".matches(r2)); // false
        System.out.println("c".matches(r2)); // false
        System.out.println("[abc]".matches(r2)); // true
    }
}

到此這篇關(guān)于Java 中的正則表達(dá)式(單字符匹配和預(yù)定義字符)的文章就介紹到這了,更多相關(guān)java正則表達(dá)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

曲松县| 洞头县| 北海市| 慈利县| 沽源县| 兴山县| 镇安县| 潜江市| 松阳县| 平南县| 井陉县| 济源市| 遵义市| 镇宁| 元阳县| 宜昌市| 教育| 饶阳县| 聂荣县| 五华县| 琼结县| 海淀区| 仪征市| 延安市| 沁水县| 徐闻县| 灵宝市| 建湖县| 滕州市| 南城县| 阿鲁科尔沁旗| 蒲城县| 水富县| 古丈县| 鄂托克前旗| 鄂州市| 都安| 大名县| 江口县| 榆树市| 沂水县|