站長資訊網
        最全最豐富的資訊網站

        詳細介紹Java正則表達式之單字符匹配和預定義字符

        本篇文章給大家帶來了關于java的相關知識,其中主要介紹了關于單字符匹配和預定義字符的相關內容,下面一起來看一下,希望對大家有幫助。

        詳細介紹Java正則表達式之單字符匹配和預定義字符

        程序員必備接口測試調試工具:立即使用
        Apipost = Postman + Swagger + Mock + Jmeter
        Api設計、調試、文檔、自動化測試工具
        后端、前端、測試,同時在線協作,內容實時同步

        推薦學習:《java視頻教程》

        一、需求

        現有一個字符串,需滿足如下要求:

        • [6, 18] 個字符

        • 只能包含字母、數字、下劃線

        • 需以字母開頭

        如果字符串滿足上述要求,返回 true,否則返回 false

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

        正則表達式用極簡的規則取代了復雜的驗證邏輯

        Regex Expression

        正則表達式是一種通用的技術,適用于多種編程語言

        二、單字符匹配(6個)

        1. [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      }}
        登錄后復制

        2. [^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     }}
        登錄后復制

        3. [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     }}
        登錄后復制

        4. [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     }}
        登錄后復制

        5. [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     }}
        登錄后復制

        6. [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     }}
        登錄后復制

        三、預定義字符(7個)

        預定義字符匹配的仍然是單個字符

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

        Java 中需以兩個【】開頭表示預定義字符

        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     }}
        登錄后復制

        推薦學習:《java視頻教程》

        贊(0)
        分享到: 更多 (0)
        網站地圖   滬ICP備18035694號-2    滬公網安備31011702889846號
        主站蜘蛛池模板: 偷拍精品视频一区二区三区| 亚洲欧美日韩另类精品一区二区三区| 精品国产青草久久久久福利| 国产亚洲精品资源在线26u| 国产专区日韩精品欧美色| 99久久人妻无码精品系列蜜桃 | 久久精品国产亚洲沈樵| 夜夜高潮夜夜爽国产伦精品| 国产精品男男视频一区二区三区| sihu国产精品永久免费| 久久丫精品国产亚洲av| 中文无码精品一区二区三区| 精品中文高清欧美| 国产精品免费久久久久影院| 免费欧美精品a在线| 国产精品揄拍100视频| 久久综合国产乱子伦精品免费 | 国产精品午睡沙发系列| 久久夜色精品国产噜噜麻豆| 亚洲国产精品成人AV无码久久综合影院| 国产精品青草久久久久福利99| 久久国产乱子伦精品免费强| 91精品在线播放| 国产成人精品优优av| 欧美成人精品高清在线观看| 亚洲AV永久无码精品水牛影视| 青青青青久久精品国产h久久精品五福影院1421| 人精品影院| 国产成人综合久久精品尤物| 91国内揄拍国内精品对白不卡| 久久精品国产精品青草| 四虎精品8848ys一区二区| 久久96国产精品久久久| 精品午夜久久福利大片| 久久精品视频网| 四虎成人精品免费影院| 久久最新精品国产| 91精品国产综合久久四虎久久无码一级| 久久精品国产91久久综合麻豆自制 | 国产精品免费一区二区三区四区| 亚洲国产精品国自产拍AV|