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正则判断包含其他字符内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • java 单播、广播、组播详解及实例代码

    java 单播、广播、组播详解及实例代码

    这篇文章主要介绍了java 单播、广播、组播详解及实例代码的相关资料,需要的朋友可以参考下
    2017-02-02
  • Spring的构造器注入全过程

    Spring的构造器注入全过程

    这篇文章主要介绍了Spring的构造器注入全过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-09-09
  • java后台接受到图片后保存方法

    java后台接受到图片后保存方法

    在本篇文章里小编给大家整理了关于java后台接受到图片后怎么保存的相关知识点,需要的朋友们参考学习下。
    2019-06-06
  • springboot集成mybatisplus实例详解

    springboot集成mybatisplus实例详解

    这篇文章主要介绍了springboot集成mybatisplus实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-09-09
  • Springboot之@ConfigurationProperties注解解读

    Springboot之@ConfigurationProperties注解解读

    在Spring Boot中,@EnableConfigurationProperties注解的主要作用是激活@ConfigurationProperties注解的配置属性类,从而让配置属性类能被Spring容器管理,这样的话,我们就可以在属性类中轻松地使用@ConfigurationProperties来绑定配置文件中的属性
    2024-10-10
  • 关于Java跨域Json字符转类对象的方法示例

    关于Java跨域Json字符转类对象的方法示例

    这篇文章主要给大家介绍了关于Java跨域Json字符转类对象的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2017-11-11
  • SpringBoot项目整合Log4j2实现自定义日志打印失效问题解决

    SpringBoot项目整合Log4j2实现自定义日志打印失效问题解决

    这篇文章主要介绍了SpringBoot项目整合Log4j2实现自定义日志打印失效问题解决,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2024-01-01
  • 在jmeter的beanshell中用java获取系统当前时间的简单实例

    在jmeter的beanshell中用java获取系统当前时间的简单实例

    这篇文章介绍了在jmeter的beanshell中用java获取系统当前时间的简单实例,有需要的朋友可以参考一下
    2013-09-09
  • Spring框架构造注入type属性实例详解

    Spring框架构造注入type属性实例详解

    这篇文章主要介绍了Spring框架构造注入type属性实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-12-12
  • Spring注解驱动开发实现属性赋值

    Spring注解驱动开发实现属性赋值

    这篇文章主要介绍了Spring注解驱动开发实现属性赋值,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-04-04

最新评论