• 解析正则表达式的语法(二)


    解析正则表达式的语法(二)

    字符匹配符

    1.1 .

    a 含义

    匹配除了\n以外的任何一个字符

    b 说明

    a.b意思以小写字母a开头,小写字母b结尾且中间包括一个任意字符的长度为3的字符串

    c 示例代码

    package Work;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    public class Test05 {
        public static void main(String[] args) {
            //1.提供需要查找的字符串
            String content="a.bat.bsnu8yg";
            //2.提供正则表达式模板字符串
            String regex="a.b";
            //3.创建正则表达式对象
            Pattern pattern=Pattern.compile(regex);
            //4.创建匹配器对象
            Matcher matcher=pattern.matcher(content);
            //5.获取并打印符合正则表达式模板字符串要求的字符串
            while(matcher.find()){
                System.out.println("成功找到"+matcher.group(0));
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    d.示例代码运行截图

    在这里插入图片描述

    1.2 \\d

    a 含义

    匹配0-9之间的任意一个数字,类似于[0-9]这样的写法

    b 说明

    \\d\\d代表两个连续的数字

    c 示例代码

    package Work;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    public class Test05 {
        public static void main(String[] args) {
            //1.提供需要查找的字符串
            String content="a.b14a4t.b89s3n4uer8yg";
            //2.提供正则表达式模板字符串
            String regex="\\d\\d";
            //3.创建正则表达式对象
            Pattern pattern=Pattern.compile(regex);
            //4.创建匹配器对象
            Matcher matcher=pattern.matcher(content);
            //5.获取并打印符合正则表达式模板字符串要求的字符串
            while(matcher.find()){
                System.out.println("成功找到"+matcher.group(0));
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    d.示例代码运行截图

    在这里插入图片描述

    1.3 \\D

    a 含义

    匹配单个非数字字符,类似于[^0-9]这样的写法

    b 说明

    \\D\\D意思是匹配连续两个非数字字符

    c 示例代码

    package Work;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    public class Test05 {
        public static void main(String[] args) {
            //1.提供需要查找的字符串
            String content="a.b1er4a4t12t1";
            //2.提供正则表达式模板字符串
            String regex="\\D\\D";
            //3.创建正则表达式对象
            Pattern pattern=Pattern.compile(regex);
            //4.创建匹配器对象
            Matcher matcher=pattern.matcher(content);
            //5.获取并打印符合正则表达式模板字符串要求的字符串
            while(matcher.find()){
                System.out.println("成功找到"+matcher.group(0));
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    d.示例代码运行截图

    在这里插入图片描述

    1.4 \\w

    a 含义

    匹配单个字母、数字、下划线

    b 说明

    \\w\\w意味着匹配两个连续的单个字符,字符有可能是字母、数字、下划线

    c 示例代码

    package Work;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    public class Test05 {
        public static void main(String[] args) {
            //1.提供需要查找的字符串
            String content="%!a1u(fkfh";
            //2.提供正则表达式模板字符串
            String regex="\\w\\w";
            //3.创建正则表达式对象
            Pattern pattern=Pattern.compile(regex);
            //4.创建匹配器对象
            Matcher matcher=pattern.matcher(content);
            //5.获取并打印符合正则表达式模板字符串要求的字符串
            while(matcher.find()){
                System.out.println("成功找到"+matcher.group(0));
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    d.示例代码运行截图

    在这里插入图片描述

    1.5 \\W

    a 含义

    匹配单个非字母、数字、下划线字符,注意这个是大写的W

    b 说明

    \\W\\W匹配两个连续的字符,字符可以是除了数字、字母、下划线以外的任意一种

    c 示例代码

    package Work;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    public class Test05 {
        public static void main(String[] args) {
            //1.提供需要查找的字符串
            String content="%!a1u(&_@kfh";
            //2.提供正则表达式模板字符串
            String regex="\\W\\W";
            //3.创建正则表达式对象
            Pattern pattern=Pattern.compile(regex);
            //4.创建匹配器对象
            Matcher matcher=pattern.matcher(content);
            //5.获取并打印符合正则表达式模板字符串要求的字符串
            while(matcher.find()){
                System.out.println("成功找到"+matcher.group(0));
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    d.示例代码运行截图

    在这里插入图片描述

    1.6 \\s

    a 含义

    匹配任意一个空白字符(空格、制表符啥的都可以)

    b 说明

    \\s\\s意味着匹配两个连续空白字符

    c 示例代码

    package Work;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    public class Test05 {
        public static void main(String[] args) {
            //1.提供需要查找的字符串
            String content="%!  a\t1u\t\t(&_@kfh";
            //2.提供正则表达式模板字符串
            String regex="\\s\\s";
            //3.创建正则表达式对象
            Pattern pattern=Pattern.compile(regex);
            //4.创建匹配器对象
            Matcher matcher=pattern.matcher(content);
            //5.获取并打印符合正则表达式模板字符串要求的字符串
            while(matcher.find()){
                //为了让打印有效果,就再group()方法前后加上下划线
                System.out.println("成功找到_"+matcher.group(0)+"_");
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    d.示例代码运行截图

    在这里插入图片描述

    1.7 \\S

    a 含义

    匹配任意一个非空白字符

    b 说明

    \\S\\S匹配两个连续的非空白字符

    c 示例代码

    package Work;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    public class Test05 {
        public static void main(String[] args) {
            //1.提供需要查找的字符串
            String content="%!  a\t1u\t\t(&_@kfh";
            //2.提供正则表达式模板字符串
            String regex="\\S\\S";
            //3.创建正则表达式对象
            Pattern pattern=Pattern.compile(regex);
            //4.创建匹配器对象
            Matcher matcher=pattern.matcher(content);
            //5.获取并打印符合正则表达式模板字符串要求的字符串
            while(matcher.find()){
                System.out.println("成功找到"+matcher.group(0)+"");
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    d.示例代码运行截图

    在这里插入图片描述

    1.8 []

    a 含义

    可接受的字符列表,里面所有符号都是本身的意思,.就是.的意思,不需要去转义

    b 说明

    [abcd] 意味着匹配a、b、c、d中的任意一个字符

    c 示例代码

    package Work;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    public class Test05 {
        public static void main(String[] args) {
            //1.提供需要查找的字符串
            String content="%!  a\t1u\t\t(&c_@dkfh";
            //2.提供正则表达式模板字符串
            String regex="[abcd]";
            //3.创建正则表达式对象
            Pattern pattern=Pattern.compile(regex);
            //4.创建匹配器对象
            Matcher matcher=pattern.matcher(content);
            //5.获取并打印符合正则表达式模板字符串要求的字符串
            while(matcher.find()){
                System.out.println("成功找到"+matcher.group(0)+"");
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    d.示例代码运行截图

    在这里插入图片描述

    1.9 [^]

    a 含义

    不接受的字符列表

    b 说明

    [^abcd]意思是匹配 除了a、b、c、d外的任意一个字符,特别需要注意的是字符也包含数字和特殊符号

    c 示例代码

    package Work;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    public class Test05 {
        public static void main(String[] args) {
            //1.提供需要查找的字符串
            String content="a1&c_dkh";
            //2.提供正则表达式模板字符串
            //2.提供正则表达式模板字符串
            String regex="[^abcd]";
            //3.创建正则表达式对象
            Pattern pattern=Pattern.compile(regex);
            //4.创建匹配器对象
            Matcher matcher=pattern.matcher(content);
            //5.获取并打印符合正则表达式模板字符串要求的字符串
            while(matcher.find()){
                System.out.println("成功找到"+matcher.group(0)+"");
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    d.示例代码运行截图

    在这里插入图片描述

    1.10 |

    a 含义

    匹配"|"之前或之后的表达式

    b 说明

    zhang|张|章 意思是匹配zhang或者张或者章,只要符合三者中的任意一种都会匹配出来

    c 示例代码

    package Work;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    public class Test05 {
        public static void main(String[] args) {
            //1.提供需要查找的字符串
            String content="zhangfdsihhf章sf张";
            //2.提供正则表达式模板字符串
            //2.提供正则表达式模板字符串
            String regex="zhang|章|张";
            //一般不放入[]里面,[]里面会把英文当成单个的字符去进行截取
            //3.创建正则表达式对象
            Pattern pattern=Pattern.compile(regex);
            //4.创建匹配器对象
            Matcher matcher=pattern.matcher(content);
            //5.获取并打印符合正则表达式模板字符串要求的字符串
            while(matcher.find()){
                System.out.println("成功找到"+matcher.group(0)+"");
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    d.示例代码运行截图

    在这里插入图片描述

    1.10 -

    a 含义

    连字符一般放在[]里面一起使用,abcd可以写成a-d

    b 说明

    [a-d]意思匹配a、b、c、d中的任意一个字符

    c 示例代码

    package Work;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    public class Test05 {
        public static void main(String[] args) {
            //1.提供需要查找的字符串
            String content="zhangfdsihhf章sf张";
            //2.提供正则表达式模板字符串
            //2.提供正则表达式模板字符串
            String regex="[a-d]";
            //一般不放入[]里面,[]里面会把英文当成单个的字符去进行截取
            //3.创建正则表达式对象
            Pattern pattern=Pattern.compile(regex);
            //4.创建匹配器对象
            Matcher matcher=pattern.matcher(content);
            //5.获取并打印符合正则表达式模板字符串要求的字符串
            while(matcher.find()){
                System.out.println("成功找到"+matcher.group(0)+"");
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    d.示例代码运行截图

    在这里插入图片描述

  • 相关阅读:
    DIY 一个汽车方向盘游戏外设(MMOS OSW DIY)
    不堆概念、换个角度聊多线程并发编程
    LabVIEW开发带式谱感测技术
    neo4j load csv 配置和使用
    【气动学】基于Matlab模拟各类导弹跟踪
    用友一面面经
    Spring笔记(四)(黑马)(web层解决方案-SpringMVC)
    二叉树之路径
    Demo25重复元素II
    小样本学习(Few-shot Learning)
  • 原文地址:https://blog.csdn.net/SSS4362/article/details/126024783