• Java使用正则表达式判断是否包含:数字、字母、特殊字符、中文


    前言

    业务需要,判断中文字符串中是否有其他字符,你可以使用表达式:数字、大小写字母、特殊符号来判断是不是纯中文,但是最简单的还是使用中文匹配规则判断。

    Matcher类的matches()和find()的区别

    • matches()方法:匹配整个区域。
    • find()方法:尝试查找与该模式匹配的输入序列的下一个子序列,简单来说就是匹配符合的目标就会返回true

    方法一:数字匹配

    纯数字匹配

    Pattern compile = Pattern.compile("[0-9]+");
    
    • 1
    • matches()方法
        public static void main(String[] args) {
            String str = "qwer2";
            Pattern compile = Pattern.compile("[0-9]+");
            Matcher matcher = compile.matcher(str);
            boolean b = matcher.matches();
            System.out.println(b);//Output: false
            str = "123";
            matcher = compile.matcher(str);
            b = matcher.matches();
            System.out.println(b);//Output: true
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    只有当字符串全是数字时,返回true

    • find()方法
        public static void main(String[] args) {
            String str = "2qw2er2";
            Pattern compile = Pattern.compile("[0-9]+");
            Matcher matcher = compile.matcher(str);
            boolean b = matcher.find();
            System.out.println(b);//Output: true
            str = "qwer";
            matcher = compile.matcher(str);
            b = matcher.find();
            System.out.println(b);//Output: false
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    只要字符串中包含数字返回true,除非字符串没有数字,则返回false

    包含数字匹配

    Pattern compile = Pattern.compile(".*[0-9].*");
    
    • 1
    • matches()方法
        public static void main(String[] args) {
            String str = "qwer2";
            Pattern compile = Pattern.compile(".*[0-9].*");
            Matcher matcher = compile.matcher(str);
            boolean b = matcher.matches();
            System.out.println(b);//Output: true
            str = "qwer";
            matcher = compile.matcher(str);
            b = matcher.matches();
            System.out.println(b);//Output: false
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    find()方法匹配结果基本一致,你可能会发现这种表达式与上面[0-9]+一样的结果,但是一定要注意,你所需要的业务需求是什么,准确的表达式更容易理解

    这种表达式很简单的告诉我们只要包含数字返回true,除非字符串没有数字,则返回false

    方法二:字母匹配

    纯字母匹配

    Pattern compile = Pattern.compile("[a-zA-Z]+");
    
    • 1
    • matches()方法
            String str = "Qwer2";
            Pattern compile = Pattern.compile("[a-zA-Z]+");
            Matcher matcher = compile.matcher(str);
            boolean b = matcher.matches();
            System.out.println(b);//Output: false
            str = "Qwer";
            matcher = compile.matcher(str);
            b = matcher.matches();
            System.out.println(b);//Output: true
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    无论字母大小写,只要字符串中全是字母,返回true,否则为false

    • find()方法
        public static void main(String[] args) {
            String str = "Qwer2";
            Pattern compile = Pattern.compile("[a-zA-Z]+");
            Matcher matcher = compile.matcher(str);
            boolean b = matcher.find();
            System.out.println(b);//Output: true
            str = "Qwer";
            matcher = compile.matcher(str);
            b = matcher.find();
            System.out.println(b);//Output: true
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    无论字母大小写,只要字符串包含字母,返回true,否则返回false

    包含字母匹配

    Pattern compile = Pattern.compile(".*[a-zA-z].*");
    
    • 1
    • matches()方法
        public static void main(String[] args) {
            String str = "Qwer2";
            Pattern compile = Pattern.compile(".*[a-zA-z].*");
            Matcher matcher = compile.matcher(str);
            boolean b = matcher.matches();
            System.out.println(b);//Output: true
            str = "1234";
            matcher = compile.matcher(str);
            b = matcher.matches();
            System.out.println(b);//Output: false
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    find()方法与matches()方法匹配结果基本一致,包含字母返回true

    方法三:特殊字符匹配

    纯特殊字符匹配

    Pattern compile = Pattern.compile("[[ _`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]|\\n|\\r|\\t]+");
    
    • 1
    • matches()方法
        public static void main(String[] args) {
            String str = "~!@#$`";
            Pattern compile = Pattern.compile("[[ _`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]|\\n|\\r|\\t]+");
            Matcher matcher = compile.matcher(str);
            boolean b = matcher.matches();
            System.out.println(b);//Output: true
            str = "~!@#$`2";
            matcher = compile.matcher(str);
            b = matcher.matches();
            System.out.println(b);//Output: false
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    字符串只有纯特殊字符才会返回true,如果包含字母或数字、等,返回false

    前面多次介绍find()方法,后续不做过多介绍。

    包含特殊字符匹配

    Pattern compile = Pattern.compile(".*[[ _`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]|\\n|\\r|\\t].*");
    
    • 1
    • matches()方法
        public static void main(String[] args) {
            String str = "~!@#$`2";
            Pattern compile = Pattern.compile(".*[[ _`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]|\\n|\\r|\\t].*");
            Matcher matcher = compile.matcher(str);
            boolean b = matcher.matches();
            System.out.println(b);//Output: true
            str = "qwer2";
            matcher = compile.matcher(str);
            b = matcher.matches();
            System.out.println(b);//Output: false
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    字符串包含特殊字符返回true,否则返回false

    方法四:数字+字母匹配

    纯数字+字母匹配

    Pattern compile = Pattern.compile("[0-9a-zA-z]+");
    
    • 1
    • matches()方法
        public static void main(String[] args) {
            String str = "Qwer2";
            Pattern compile = Pattern.compile("[0-9a-zA-z]+");
            Matcher matcher = compile.matcher(str);
            boolean b = matcher.matches();
            System.out.println(b);//Output: true
            str = "Qwer2~";
            matcher = compile.matcher(str);
            b = matcher.matches();
            System.out.println(b);//Output: false
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    字符串为数字或大小写字母返回true,否则返回false

    包含数字+字母匹配

    Pattern compile = Pattern.compile(".*[0-9a-zA-z].*");
    
    • 1
    • matches()方法
        public static void main(String[] args) {
            String str = "Qwer2%";
            Pattern compile = Pattern.compile(".*[0-9a-zA-z].*");
            Matcher matcher = compile.matcher(str);
            boolean b = matcher.matches();
            System.out.println(b);//Output: true
            str = "~@#";
            matcher = compile.matcher(str);
            b = matcher.matches();
            System.out.println(b);//Output: false
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    字符串包含数字或大小写字母返回true,否则返回false

    方法五:中文匹配

    纯中文匹配

    Pattern compile = Pattern.compile("[\\u4e00-\\u9fa5]+");
    
    • 1
    • matches()方法
        public static void main(String[] args) {
            String str = "中文";
            Pattern compile = Pattern.compile("[\\u4e00-\\u9fa5]+");
            Matcher matcher = compile.matcher(str);
            boolean b = matcher.matches();
            System.out.println(b);//Output: true
            str = "中文2";
            matcher = compile.matcher(str);
            b = matcher.matches();
            System.out.println(b);//Output: false
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    字符串为纯中文返回true,否则返回false

    包含中文匹配

    Pattern compile = Pattern.compile(".*[\\u4e00-\\u9fa5].*");
    
    • 1
    • matches()方法
        public static void main(String[] args) {
            String str = "中文2";
            Pattern compile = Pattern.compile(".*[\\u4e00-\\u9fa5].*");
            Matcher matcher = compile.matcher(str);
            boolean b = matcher.matches();
            System.out.println(b);//Output: true
            str = "qwer2";
            matcher = compile.matcher(str);
            b = matcher.matches();
            System.out.println(b);//Output: false
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    字符串包含中文返回true,否则返回false

    正则表达式

    通过上面的介绍,你可能会发现一个规律,[匹配规则]+表示完整匹配、.*[匹配规则].*表示包含匹配,只要替换里面规则就能实现对应的操作,但是并不理解它们的含义,下面介绍正则表达式的一些语句以及含义。

    • [ABC]:匹配 [...] 中的所有字符。
    • [^ABC]:匹配除了 [...] 中字符的所有字符。
    • [A-Z]:表示一个区间,匹配所有大写字母,[a-z] 表示所有小写字母。
    • *:匹配前面的子表达式零次或多次。例如,zo* 能匹配 “z” 以及 “zoo”。
    • +:匹配前面的子表达式一次或多次。例如,zo+ 能匹配 “zo” 以及 “zoo”,但不能匹配 “z”。
    • .:匹配除换行符 \n 之外的任何单字符。
    • ?:匹配前面的子表达式零次或一次。

    *+ 限定符都是贪婪的,因为它们会尽可能多的匹配文字。

  • 相关阅读:
    使用 OpenCV 收集数据
    R语言ggplot2可视化:使用ggpubr包的ggline函数可视化折线图(点线图、line plot)
    Centos7,yum安装mysql
    centos环境用docker安装jenkins相关命令
    注意:Spring Boot 2.7开始spring.factories不推荐使用了,接下来这么玩...
    基于自然语言处理的结构化数据库问答机器人系统
    浏览器工作原理分析与首屏加载
    python字符串常用方法介绍,基于python3.10
    2022年湖北七大员发证单位是哪里?甘建二告诉你
    适用于 Windows 10 和 Windows 11 设备的笔记本电脑管理软件
  • 原文地址:https://blog.csdn.net/qq_39940674/article/details/127851457