• 正则表达式


    正则表达式-字符类

    1. - 语法示例:
    2. 1. \[abc\]:代表a或者b,或者c字符中的一个。
    3. 2. \[^abc\]:代表除a,b,c以外的任何字符。
    4. 3. [a-z]:代表a-z的所有小写字符中的一个。
    5. 4. [A-Z]:代表A-Z的所有大写字符中的一个。
    6. 5. [0-9]:代表0-9之间的某一个数字字符。
    7. 6. [a-zA-Z0-9]:代表a-z或者A-Z或者0-9之间的任意一个字符。
    8. 7. [a-d[m-p]]:a 到 d 或 m 到 p之间的任意一个字符。

    测试

    1. package com.itheima.a08regexdemo;
    2. public class RegexDemo2 {
    3. public static void main(String[] args) {
    4. //public boolean matches(String regex):判断是否与正则表达式匹配,匹配返回true
    5. // 只能是a b c
    6. System.out.println("-----------1-------------");
    7. System.out.println("a".matches("[abc]")); // true
    8. System.out.println("z".matches("[abc]")); // false
    9. // 不能出现a b c
    10. System.out.println("-----------2-------------");
    11. System.out.println("a".matches("[^abc]")); // false
    12. System.out.println("z".matches("[^abc]")); // true
    13. System.out.println("zz".matches("[^abc]")); //false
    14. System.out.println("zz".matches("[^abc][^abc]")); //true
    15. // a到zA到Z(包括头尾的范围)
    16. System.out.println("-----------3-------------");
    17. System.out.println("a".matches("[a-zA-z]")); // true
    18. System.out.println("z".matches("[a-zA-z]")); // true
    19. System.out.println("aa".matches("[a-zA-z]"));//false
    20. System.out.println("zz".matches("[a-zA-Z]")); //false
    21. System.out.println("zz".matches("[a-zA-Z][a-zA-Z]")); //true
    22. System.out.println("0".matches("[a-zA-Z]"));//false
    23. System.out.println("0".matches("[a-zA-Z0-9]"));//true
    24. // [a-d[m-p]] a到d,或m到p
    25. System.out.println("-----------4-------------");
    26. System.out.println("a".matches("[a-d[m-p]]"));//true
    27. System.out.println("d".matches("[a-d[m-p]]")); //true
    28. System.out.println("m".matches("[a-d[m-p]]")); //true
    29. System.out.println("p".matches("[a-d[m-p]]")); //true
    30. System.out.println("e".matches("[a-d[m-p]]")); //false
    31. System.out.println("0".matches("[a-d[m-p]]")); //false
    32. // [a-z&&[def]] a-z和def的交集。为:d,e,f
    33. System.out.println("----------5------------");
    34. System.out.println("a".matches("[a-z&[def]]")); //false
    35. System.out.println("d".matches("[a-z&&[def]]")); //true
    36. System.out.println("0".matches("[a-z&&[def]]")); //false
    37. // [a-z&&[^bc]] a-z和非bc的交集。(等同于[ad-z])
    38. System.out.println("-----------6------------_");
    39. System.out.println("a".matches("[a-z&&[^bc]]"));//true
    40. System.out.println("b".matches("[a-z&&[^bc]]")); //false
    41. System.out.println("0".matches("[a-z&&[^bc]]")); //false
    42. // [a-z&&[^m-p]] a到z和除了m到p的交集。(等同于[a-1q-z])
    43. System.out.println("-----------7-------------");
    44. System.out.println("a".matches("[a-z&&[^m-p]]")); //true
    45. System.out.println("m".matches("[a-z&&[^m-p]]")); //false
    46. System.out.println("0".matches("[a-z&&[^m-p]]")); //false
    47. }
    48. }

     正则表达式-预定义字符

    1. - 语法示例:
    2. 1. "." : 匹配任何字符。
    3. 2. "\\d":任何数字[0-9]的简写;
    4. 3. "\D":任何非数字\[^0-9\]的简写;
    5. 4. "\\s": 空白字符:[ \t\n\x0B\f\r] 的简写
    6. 5. "\S": 非空白字符:\[^\s\] 的简写
    7. 6. "\\w":单词字符:[a-zA-Z_0-9]的简写
    8. 7. "\W":非单词字符:\[^\w\]
    1. public class Demo {
    2. public static void main(String[] args) {
    3. //.表示任意一个字符
    4. System.out.println("你".matches("..")); //false
    5. System.out.println("你".matches(".")); //true
    6. System.out.println("你a".matches(".."));//true
    7. // \\d 表示任意的一个数字
    8. // \\d只能是任意的一位数字
    9. // 简单来记:两个\表示一个\
    10. System.out.println("a".matches("\\d")); // false
    11. System.out.println("3".matches("\\d")); // true
    12. System.out.println("333".matches("\\d")); // false
    13. //\\w只能是一位单词字符[a-zA-Z_0-9]
    14. System.out.println("z".matches("\\w")); // true
    15. System.out.println("2".matches("\\w")); // true
    16. System.out.println("21".matches("\\w")); // false
    17. System.out.println("你".matches("\\w"));//false
    18. // 非单词字符
    19. System.out.println("你".matches("\\W")); // true
    20. System.out.println("---------------------------------------------");
    21. // 以上正则匹配只能校验单个字符。
    22. // 必须是数字 字母 下划线 至少 6位
    23. System.out.println("2442fsfsf".matches("\\w{6,}"));//true
    24. System.out.println("244f".matches("\\w{6,}"));//false
    25. // 必须是数字和字符 必须是4位
    26. System.out.println("23dF".matches("[a-zA-Z0-9]{4}"));//true
    27. System.out.println("23 F".matches("[a-zA-Z0-9]{4}"));//false
    28. System.out.println("23dF".matches("[\\w&&[^_]]{4}"));//true
    29. System.out.println("23_F".matches("[\\w&&[^_]]{4}"));//false
    30. }
    31. }

    正则表达式-数量词

    1. 语法示例:
    2. 1. X? : 0次或1
    3. 2. X* : 0次到多次
    4. 3. X+ : 1次或多次
    5. 4. X{n} : 恰好n次
    6. 5. X{n,} : 至少n次
    7. 6. X{n,m}: n到m次(n和m都是包含的)
    1. public class Demo {
    2. public static void main(String[] args) {
    3. // 必须是数字 字母 下划线 至少 6位
    4. System.out.println("2442fsfsf".matches("\\w{6,}"));//true
    5. System.out.println("244f".matches("\\w{6,}"));//false
    6. // 必须是数字和字符 必须是4位
    7. System.out.println("23dF".matches("[a-zA-Z0-9]{4}"));//true
    8. System.out.println("23 F".matches("[a-zA-Z0-9]{4}"));//false
    9. System.out.println("23dF".matches("[\\w&&[^_]]{4}"));//true
    10. System.out.println("23_F".matches("[\\w&&[^_]]{4}"));//false
    11. }
    12. }

  • 相关阅读:
    《护理管理学》习题及答案-考试版
    借降本增效之名,探索开闭原则架构设计
    CH59X/CH58X/CH57X PWM使用
    git提交代码实际操作
    公共用房管理系统有哪些功能和范围?
    Rust的注释与文档
    [ROS系列]ubuntu 20.04 从零配置orbslam3(无坑版)
    C++的类型转换
    JDK -- IO
    基于非支配排序遗传算法的多目标水光互补优化调度附Matlab代码
  • 原文地址:https://blog.csdn.net/m0_73967798/article/details/134338406