• 正则


    正则表达式简介

    实际开发中,经常需要对字符串数据进行一-些复 杂的匹配、查找、替换等操作。通过“正则表达式"
    可以方便的实现字符串的复杂操作。
    正则表达式是一串特定字符,组成一个"规则字符串”这个"规则字符串”是描述文本规则的工具。正则表达式就是记录文本规则的代码。
    例如:
    -正则表达式: "[a-Z]" 表示a到z的任意一个字符。
    -正则表达式:"[a-z]+" 表示由1个或多个a-z字符组成的字符串。

    一、字符集合

        /**测试正则表达式:字符集合*/
        public void testRegEx3() {
            String regex1 = "[a-z]";
            String regex2 = "[^a-z]";
            String regex3 = "[a-z&&[^bc]";
            String regex4 = "\d";// [0-9]
            String regex5 = "\D";// [^0-9]
            String regex6 = "\s";// [ 	
    x0Bf
    ]
            String regex7 = "\S ";// [^s]
            String regex8 = "\w";// [a-zA-Z_ _0-9]
            String regex9 = "\W";// [^w]
    
            /**测试正则表达式:字符集合*/
            System.out.println("a" .matches(regex1));// true
            System.out.println("b".matches(regex2));// false
            System.out.println("f".matches(regex3));// true
            System.out.println("b".matches(regex3));// false
            System.out.println(" 1" .matches(regex4));// true
            System.out.println("1".matches(regex5));// false
            System.out.println("".matches(regex6));// true
            System.out.println("".matches(regex7));// false
            System.out.println("_".matches(regex8));// true
            System.out.println("_" .matches(regex9));// false
    
        }
    

    二、预定义字符集

    三、数量词

        /** *测试正则表达式:"*"、"+"、"?" */
        public void testRegEx1() {
            String regex1 = "[a-z]*";
            String regex2 = "\w+\.jar";
            String regex3 = "\d?[a-z]*";
            System.out.println("abcde ".matches(regex1)); // true
            System.out.println("".matches(regex1)); // true
            System.out.println("lang.jar".matches(regex2)); // true
            System.out.println("jar".matches(regex2)); // false
            System.out.println("1abc".matches(regex3)); // true
            System.out.println("123abc".matches(regex3)); //false
            System.out.println("abc".matches(regex3)); // true
        }
        /** 测试正则表达式: "{n}"、 "{n,}"、 "{n,m} “*/
        public void testRegEx2() {
            String regex1 = "\w{5}";
            String regex2 = "\d{5,}";
            String regex3 = "[a-zA-Z]{5,8}";
            System.out.println("abcef".matches(regex1));// true
            System.out.println("abcefg".matches(regex1));// false 
            System.out.println("12345".matches(regex2));// true
            System.out.println("123456".matches(regex2)); // true
            System.out.println("1234".matches(regex2));// false 
            System.out.println("Hello".matches(regex3));// true
            System.out.println("HelloWorld".matches(regex3)); // false
        }
    

    四、分组"()"


    五、"^" 和 "$"

    拓展

    ? 问号表示
    如果前面是单个字符,则表示0或1个,即有有或没有
    如果前面是连续匹配的多个字符,则表示匹配到的个数尽可能

    举例

    <.+> 和 <.+?>

    含义解析: .+表示任何字符,个数上是 >=1,加上?表示个数尽可能的少。.+?表示最小匹配

    <a href="xxx"><span>
    
    如果用<.+>匹配,则匹配结果是:<a href="xxx"><span>
    如果用<.+?>匹配,则匹配结果是:<a href="xxx">
    也就是.+?只要匹配就返回了,不会再接着往下找了
  • 相关阅读:
    深入理解Java8中Stream的实现原理
    RocketMQ的顺序消费和事务消费
    Java 性能调优小技巧
    类加载机制基础
    十大经典排序算法
    分布式锁的几种常用实现方式
    python之接口开发
    python之urllib模块和requests模块
    python之time模块和hashlib模块
    python之os和sys模块的区别
  • 原文地址:https://www.cnblogs.com/wangyuyanhello/p/14084169.html
Copyright © 2020-2023  润新知