38、Java 中的正则表达式(单字符匹配和预定义字符)

简介: 38、Java 中的正则表达式(单字符匹配和预定义字符)


一、需求

❓ 现有一个字符串,需满足如下要求:

[6, 18] 个字符

② 只能包含字母、数字、下划线

③ 需以字母开头

❓ 如果字符串满足上述要求,返回 true,否则返回 false

public static boolean validString(String s) {
       return s.matches("[a-zA-Z][a-zA-Z0-9_]{5,17}");
   }

🍀 正则表达式用极简的规则取代了复杂的验证逻辑

🍀 Regex Expression

🍀 正则表达式是一种通用的技术,适用于多种编程语言

二、单字符匹配(6个)

✏️ ① [abc]:字符串的某个位置(某一个字符)满足 a、b、c 中的一个

🍀 某个位置:该【单字符匹配】放的位置

public class TestDemo {
    public static void main(String[] args) {
        String regex = "[zgq]";
        System.out.println("z".matches(regex)); // true
        System.out.println("g".matches(regex)); // true
        System.out.println("q".matches(regex)); // true
        System.out.println("zgq".matches(regex)); // false
    }
}
public class TestDemo {
    public static void main(String[] args) {
        String regex = "26[abc]3q";
        System.out.println("26a3q".matches(regex)); // true
        System.out.println("26abc".matches(regex)); // false
        System.out.println("26b3q".matches(regex)); // true 
    }
}

✏️ ② [^abc]:除了 a、b、c 之外的任意单个字符

public class TestDemo {
    public static void main(String[] args) {
        String regex = "[^cat]666";
        System.out.println("c666".matches(regex)); // false
        System.out.println("a666".matches(regex)); // false
        System.out.println("t666".matches(regex)); // false
        System.out.println("bb666".matches(regex)); // false
        System.out.println("b666".matches(regex)); // true
    }
}
public class TestDemo {
    public static void main(String[] args) {
        String regex1 = "[12345]666";
        String regex2 = "[^1-5]666";
        System.out.println("1666".matches(regex1)); // true
        System.out.println("3666".matches(regex1)); // true
        System.out.println("5666".matches(regex1)); // true
        System.out.println("6666".matches(regex1)); // false
        System.out.println("1666".matches(regex2)); // false
        System.out.println("3666".matches(regex2)); // false
        System.out.println("5666".matches(regex2)); // false
        System.out.println("6666".matches(regex2)); // true
    }
}

✏️ ③ [a-zA-z]:匹配单个英文字母

public class TestDemo {
    public static void main(String[] args) {
        String regex = "[a-zA-Z]666";
        System.out.println("6666".matches(regex)); // false
        System.out.println("b666".matches(regex)); // true
    }
}

✏️ ④ [a-d[1-6]]:和 [a-d1-6] 一样(并集)

public class TestDemo {
    public static void main(String[] args) {
        String regex1 = "[a-d[1-6]]";
        String regex2 = "[a-d1-6]";
        System.out.println("a".matches(regex1)); // true
        System.out.println("e".matches(regex1)); // false
        System.out.println("1".matches(regex1)); // true
        System.out.println("7".matches(regex1)); // false
        System.out.println("a".matches(regex2)); // true
        System.out.println("e".matches(regex2)); // false
        System.out.println("1".matches(regex2)); // true
        System.out.println("7".matches(regex2)); // false
    }
}

✏️ ⑤ [zgq&&[god]]:交集

public class TestDemo {
    public static void main(String[] args) {
        String regex1 = "[zgq&&[god]]";
        System.out.println("q".matches(regex1)); // false
        System.out.println("d".matches(regex1)); // false
        System.out.println("g".matches(regex1)); // true
    }
}

✏️ ⑥ [zgq&&[god]]:取差集

public class TestDemo {
    public static void main(String[] args) {
        String regex1 = "[zgq&&[^god]]";
        System.out.println("q".matches(regex1)); // true
        System.out.println("d".matches(regex1)); // false
        System.out.println("g".matches(regex1)); // false
        System.out.println("z".matches(regex1)); // true
        // 取差集, 从字母 a 到字母 z 中去除字母 b 和 d
        String regex2 = "[a-z&&[^bd]]";
        System.out.println("d".matches(regex2)); // false
        System.out.println("a".matches(regex2)); // true
    }
}

三、预定义字符(7个)

预定义字符匹配的仍然是单个字符

📝 【.】:任意单个字符

📝 【\d】:数字

📝 【\D】:非数字

📝 【\s】:空白

📝 【\S】:非空白

📝 【\w】:字母(英文字母、下划线、数字)

📝 【\W】:非英文字母

🍀 Java 中需以两个【\】开头表示预定义字符

public class TestDemo {
    public static void main(String[] args) {
        String r1 = ".";
        System.out.println("@".matches(r1)); // true
        System.out.println("庆".matches(r1)); // true
        System.out.println("I".matches(r1)); // true
        System.out.println(" ".matches(r1)); // true
        System.out.println(".".matches(r1)); // true
    }
}
public class TestDemo {
    public static void main(String[] args) {
        // 匹配 java 文件
        String r1 = ".\\.java";
        System.out.println("a.java".matches(r1)); // true
        System.out.println("xjava".matches(r1)); // false
        System.out.println("5java".matches(r1)); // false
    }
}
public class TestDemo {
    public static void main(String[] args) {
        String r1 = "[abc]";
        String r2 = "\\[abc\\]";
        System.out.println("a".matches(r1)); // true
        System.out.println("c".matches(r1)); // true
        System.out.println("[abc]".matches(r1)); // false
        System.out.println("a".matches(r2)); // false
        System.out.println("c".matches(r2)); // false
        System.out.println("[abc]".matches(r2)); // true
    }
}

结束!如有错误,请不吝赐教

相关文章
|
2月前
|
Java
Java——编码GBK的不可映射字符
Java——编码GBK的不可映射字符
34 1
|
2月前
正则表达式的限定符、或运算符、字符类、元字符、贪婪/懒惰匹配
本文介绍了正则表达式中的限定符、或运算符、字符类、元字符以及贪婪与懒惰匹配的概念和用法。
34 5
|
2月前
|
Python
【Python】正则表达式判断是否存在连续相同的两个字符,连续两个字符一模一样
Python函数isContinuousChar,使用正则表达式来检测字符串中是否存在连续的相同字母或数字,并返回存在此类字符的列表长度,如果列表长度为0则表示不存在连续相同的字符。
101 2
|
2月前
|
存储 Java Apache
|
3月前
|
Java Perl
Java进阶之正则表达式
【7月更文挑战第17天】正则表达式(RegEx)是一种模式匹配工具,用于在字符串中执行搜索、替换等操作。它由普通字符和特殊元字符组成,后者定义匹配规则。
25 4
|
2月前
|
监控 Java API
|
3月前
|
数据采集 监控 Java
day21:Java零基础 - 正则表达式
【7月更文挑战第21天】🏆本文收录于「滚雪球学Java」专栏,专业攻坚指数级提升,希望能够助你一臂之力,帮你早日登顶实现财富自由🚀;同时,欢迎大家关注&&收藏&&订阅!持续更新中,up!up!up!!
25 1
|
3月前
|
Java
如何在Java中使用正则表达式
如何在Java中使用正则表达式
下一篇
无影云桌面