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

相关文章

最新评论