Java正则表达式判断是否包含数字、字母、特殊字符及中文的多种方法

 更新时间:2023年08月01日 11:38:32   作者:成为大佬先秃头  
这篇文章主要给大家介绍了关于Java正则表达式判断是否包含数字、字母、特殊字符及中文的多种方法,Java正则表达式在字符串处理和模式匹配中扮演着重要角色,文中通过实例代码介绍的非常详细,需要的朋友可以参考下

前言

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

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

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

方法一:数字匹配

纯数字匹配

Pattern compile = Pattern.compile("[0-9]+");
  • 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
    }

只有当字符串全是数字时,返回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
    }

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

  • 包含数字匹配
Pattern compile = Pattern.compile(".*[0-9].*");

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
    }

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

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

方法二:字母匹配

纯字母匹配

Pattern compile = Pattern.compile("[a-zA-Z]+");
  • 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

无论字母大小写,只要字符串中全是字母,返回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
    }

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

包含字母匹配

Pattern compile = Pattern.compile(".*[a-zA-z].*");
  • 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
    }

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

方法三:特殊字符匹配

纯特殊字符匹配

Pattern compile = Pattern.compile("[[ _`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“'。,、?]|\\n|\\r|\\t]+");
  • 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
    }

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

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

包含特殊字符匹配

Pattern compile = Pattern.compile(".*[[ _`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“'。,、?]|\\n|\\r|\\t].*");
  • 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
    }

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

方法四:数字+字母匹配

纯数字+字母匹配

Pattern compile = Pattern.compile("[0-9a-zA-z]+");
  • 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
    }

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

包含数字+字母匹配

Pattern compile = Pattern.compile(".*[0-9a-zA-z].*");
  • 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
    }

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

方法五:中文匹配

纯中文匹配

Pattern compile = Pattern.compile("[\\u4e00-\\u9fa5]+");
  • 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
    }

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

包含中文匹配

Pattern compile = Pattern.compile(".*[\\u4e00-\\u9fa5].*");
  • 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
    }

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

正则表达式

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

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

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

总结

到此这篇关于Java正则表达式判断是否包含数字、字母、特殊字符及中文的多种方法的文章就介绍到这了,更多相关Java正则判断包含其他字符内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Spring @Bean注解深入分析源码执行过程

    Spring @Bean注解深入分析源码执行过程

    随着SpringBoot的流行,我们现在更多采用基于注解式的配置从而替换掉了基于XML的配置,所以本篇文章我们主要探讨基于注解的@Bean以及和其他注解的使用
    2023-01-01
  • SpringMvc响应数据及结果视图实现代码

    SpringMvc响应数据及结果视图实现代码

    这篇文章主要介绍了SpringMvc响应数据及结果视图实现代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-08-08
  • springboot @JsonSerialize的使用讲解

    springboot @JsonSerialize的使用讲解

    这篇文章主要介绍了springboot @JsonSerialize的使用,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-10-10
  • Java线程安全状态专题解析

    Java线程安全状态专题解析

    线程安全是多线程编程时的计算机程序代码中的一个概念。在拥有共享数据的多条线程并行执行的程序中,线程安全的代码会通过同步机制保证各个线程都可以正常且正确的执行,不会出现数据污染等意外情况
    2022-03-03
  • 简单的java图片处理类(图片水印 图片缩放)

    简单的java图片处理类(图片水印 图片缩放)

    本图片处理类功能非常之强大可以实现几乎所有WEB开发中对图像的处理功能都集成了,包括有缩放图像、切割图像、图像类型转换、彩色转黑白、文字水印、图片水印等功能
    2013-11-11
  • SpringData JPA基本/高级/多数据源的使用详解

    SpringData JPA基本/高级/多数据源的使用详解

    这篇文章主要介绍了SpringData JPA基本/高级/多数据源的使用详解,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-02-02
  • 详解阿里云maven镜像库配置(gradle,maven)

    详解阿里云maven镜像库配置(gradle,maven)

    这篇文章主要介绍了详解阿里云maven镜像库配置(gradle,maven),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-02-02
  • 深入剖析理解AsyncGetCallTrace源码底层原理

    深入剖析理解AsyncGetCallTrace源码底层原理

    这篇文章主要为大家介绍了AsyncGetCallTrace源码的深层原理,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步
    2022-02-02
  • 为IntelliJ IDEA配置JVM参数的两种方法

    为IntelliJ IDEA配置JVM参数的两种方法

    在使用IntelliJ IDEA进行Java开发时,合理配置JVM参数对于优化项目性能和资源管理至关重要,IntelliJ IDEA提供了两种方便的方式来设置JVM参数,本文将详细介绍这两种方法:通过工具栏编辑配置和通过服务编辑配置,需要的朋友可以参考下
    2024-12-12
  • Mybatis核心配置文件加载流程详解

    Mybatis核心配置文件加载流程详解

    本文将介绍MyBatis在配置文件加载的过程中,如何加载核心配置文件、如何解析映射文件中的SQL语句以及每条SQL语句如何与映射接口的方法进行关联,具有一定的参考价值,感兴趣的可以了解一下
    2023-12-12

最新评论