欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java正则表达式 判断字符串是否含有字母

程序员文章站 2022-03-25 08:52:01
...

 1 判断字符串是否含有字母

            if (Pattern.matches(".*[A-Za-z]+.*", IP)) {//"1e1.4.5.6"匹配
            if (Pattern.matches(".*[a-zA-Z]+.*", IP)) {//"1e1.4.5.6"匹配
            if (Pattern.matches(".*([A-Z]|[a-z])+.*", IP)) {//"1e1.4.5.6"匹配,需要加()
            if (Pattern.matches(".*[A-Z]|[a-z]+.*", IP)) {//"1e1.4.5.6"不匹配
            if (Pattern.matches(".*[A-Z][a-z]+.*", IP)) {//"1e1.4.5.6"不匹配
            if (Pattern.matches(".*[a-z][A-Z]+.*", IP)) {//"1e1.4.5.6"不匹配

 

        System.out.println(Pattern.matches(".*([A-Z]|[a-z])+.*", "12313"));//false
        System.out.println(Pattern.matches(".*([A-Z]|[a-z])+.*", "12313aa"));//true
        System.out.println(Pattern.matches(".*([A-Z]|[a-z])+.*", "12313Aa"));//true
        System.out.println(Pattern.matches(".*([A-Z]|[a-z])+.*", "12AA313"));//true
        System.out.println(Pattern.matches(".*([A-Z]|[a-z])+.*", "aA12313"));//true
        System.out.println(Pattern.matches(".*([A-Z]|[a-z])+.*", "aA.12313"));//true

        System.out.println(Pattern.matches(".*[A-Za-z]+.*", "12313"));//false
        System.out.println(Pattern.matches(".*[A-Za-z]+.*", "12313"));//false
        System.out.println(Pattern.matches(".*[A-Za-z]+.*", "12313aa"));//true
        System.out.println(Pattern.matches(".*[A-Za-z]+.*", "12313Aa"));//true
        System.out.println(Pattern.matches(".*[A-Za-z]+.*", "12AA313"));//true
        System.out.println(Pattern.matches(".*[A-Za-z]+.*", "aA12313"));//true
        System.out.println(Pattern.matches(".*[A-Za-z]+.*", "aA.12313"));//true

        System.out.println(Pattern.matches(".*[a-zA-Z]+.*", "12313"));//false
        System.out.println(Pattern.matches(".*[a-zA-Z]+.*", "12313aa"));//true
        System.out.println(Pattern.matches(".*[a-zA-Z]+.*", "12313Aa"));//true
        System.out.println(Pattern.matches(".*[a-zA-Z]+.*", "12AA313"));//true
        System.out.println(Pattern.matches(".*[a-zA-Z]+.*", "aA12313"));//true
        System.out.println(Pattern.matches(".*[a-zA-Z]+.*", "aA.12313"));//true

2 [a-z][A-Z]分别代表不同的两个字符

if (Pattern.matches(".*[a-z][A-Z]+.*", IP)) {//"1eA.4.5.6"匹配
if (Pattern.matches(".*[A-Z][a-z]+.*", IP)) {//"1Ae.4.5.6"匹配
        System.out.println(Pattern.matches(".*[a-z][A-Z]+.*", "1eA.4.5.6"));//true
        System.out.println(Pattern.matches(".*[A-Z][a-z]+.*", "1eA.4.5.6"));//false
        System.out.println(Pattern.matches(".*[A-Z][a-z]+.*", "1Ae.4.5.6"));//true

以上基于jdk1.8