• 正则表达式


    (一)概述
    1、就是一个字符串
    2、作用:不仅表示一个字符串,还可以表示一类字符串,表示一类字符串的规则或者格式。
    3、好处:可以使用特别简单的代码,表示非常复杂的规则。
    4、坏处:写出的正则表达式正确率较低
    5、需求:
    键盘录入一个字符串,判断是否是一个合法的QQ号码
    合法QQ号码的条件:
    必须全都是数字
    必须在5-15位
    0数字不能开头
    代码示例
    `
    import java.util.Scanner;
    /**

    • 键盘录入一个字符串,判断是否是一个合法的QQ号码
    • 合法QQ号码的条件:
    • 必须全都是数字
    • 必须在5-15位
    • 0数字不能开头
      */
      public class Demo01_Regex {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
    
        System.out.println("请录入QQ:");
        String qq = sc.nextLine();
    
        //System.out.println(isQQ(qq));
    
        System.out.println(isQQByRegex(qq));
    }
    
    private static boolean isQQByRegex(String qq) {
    
        return qq.matches("[1-9][0-9]{4,14}");
    }
    
    public static boolean isQQ(String qq) {
        //必须全都是数字
        if(!isAllNum(qq)) {
            return false;
        }
    
        //必须在5-15位
        if(!legalLength(qq)) {
            return false;
        }
    
        //0数字不能开头
        if(startZero(qq)) {
            return false;
        }
    
        return true;
    }
    
    //0数字不能开头
    private static boolean startZero(String qq) {//"0123456"
        return qq.startsWith("0");
    }
    
    //必须在5-15位
    private static boolean legalLength(String qq) {
        return qq.length() >= 5 && qq.length() <= 15;
    }
    
    //必须全都是数字
    private static boolean isAllNum(String qq) {
        char[] arr = qq.toCharArray();
    
        for (int i = 0; i < arr.length; i++) {
            char ch = arr[i];
    
            if(!(ch >= '0' && ch <= '9')) {
                return false;
            }
        }
    
        return true;
    }
    

    }
    `
    (二)正则表达式的字符类
    1、普通的字符串就是一个正则表达式,但是只能表达自己本身,无法匹配一类字符串
    2、判断某个字符串是否和某个正则表达式的规则匹配,使用String类中的matches方法
    3、字符类型:表示单个的字符,使用的符号是中括号[]
    说明:只要使用了方括号,无论里面写了多少内容,都表示单个的字符
    4、方括号的表达方式:
    [abc]:a或者b或者c中的一个
    [^abc]:除了a、b、c以外的任何单个字符
    [a-zA-Z]:az和AZ中的任意单个字符

    (三)预定义字符类
    1、有一些字符类型经常使用,所以就提前在正则表达式中进行定义,称为预定义字符类
    2、罗列:
    . 表示任意字符,\.表示一个确定的.字符串
    \d 表示数字字符
    \D 表示非数字字符
    \s 表示空格字符
    \S 表示非空格字符
    \w [a-zA-Z0-9_]
    \W 表示除了\w以外的所有字符
    `
    public class Demo03_Definetion {

    public static void main(String[] args) {
        /*
        * 空白字符:空格、制表符 \t,回车\r、换行\n
        * */
        String regex = "\\w";
    
        System.out.println("1".matches(regex));
        System.out.println("v".matches(regex));
        System.out.println("_".matches(regex));
        System.out.println("$".matches(regex));
    }
    
    public static void test4() {
        String regex = "\\D";
    
        System.out.println("5".matches(regex));
        System.out.println("51".matches(regex));
        System.out.println("a".matches(regex));
    }
    
    public static void test3() {
        String regex = "\\d";
    
        System.out.println("5".matches(regex));
        System.out.println("51".matches(regex));
        System.out.println("a".matches(regex));
    }
    
    public static void test2() {
        String regex = "\\.";
        System.out.println(" ".matches(regex));//false
        System.out.println("a".matches(regex));//false
        System.out.println(".".matches(regex));//true
    }
    
    public static void test1() {
        String regex = ".";
    
        System.out.println(" ".matches(regex));//true
        System.out.println("a".matches(regex));//true
        System.out.println("abc".matches(regex));//false
        System.out.println("q".matches(regex));//true
        System.out.println("1".matches(regex));//true
        System.out.println("&".matches(regex));//true
        System.out.println("    ".matches(regex));
    }
    

    }
    `

    (四)数量词
    1、无论是字符类型还是预定义字符类型都只能表示单个的字符,无法表示0个字符,也无法表示多个字符。需要使用一个数量词来修饰字符的个数。
    2、注意事项:数量词修饰的是紧挨在自己前面的那个字符,与其他字符类型无关
    3、分类:
    模糊的数量词
    精确的数量词
    4、模糊的数量词
    X? 表示X这个字符,出现0次或者1次
    X+ 表示X这个字符,出现1次或者多次
    X* 表示X这个字符,出现0次、1次或者多次
    5、精确的数量词
    X{n} 表示X这个字符,恰好出现n次
    X{n,} 表示X这个字符,至少出现n次
    X{n, m} 表示X这个字符,至少出现n次,最多出现m次

    `public class Demo04_Number {

    public static void main(String[] args) {
        //数量词,只能修饰紧挨着它的左侧的那一个
        String regex = "abc+[a-dA-D]?\\d+";
    
        System.out.println("aabbcc123".matches(regex));//false
    }
    
    public static void test2() {
        String regex = "\\s?\\w*[^qwe]a?";
    
        System.out.println(" ".matches(regex));//true
        System.out.println("  ".matches(regex));//true
        System.out.println("1_235".matches(regex));//true
        System.out.println("1_2aa".matches(regex));//true
        System.out.println("1_2a".matches(regex));//true
        System.out.println(" 324qqa".matches(regex));//true
        System.out.println(" 324qa".matches(regex));//true
    }
    
    public static void test1() {
        String regex = "[^qwe]?";
    
        System.out.println(" ".matches(regex));//true
        System.out.println("".matches(regex));//true
        System.out.println("a".matches(regex));//true
        System.out.println("a1".matches(regex));//false
        System.out.println("qq".matches(regex));//false
        System.out.println("q".matches(regex));//false
    }
    

    }
    `

    (五)字符串中和正则表达式有关的三个方法
    1、boolean matches(String regex):判断当前字符串是否和参数正则表达式匹配
    2、String[] split(String regex):使用指定的正则表达式切割当前字符串
    3、String replaceAll(String regex, String replacement):将调用者字符串中的所有匹配regex正则的子串,全都替换成replacement新串
    `
    import java.util.Arrays;

    public class Demo05_Method {

    public static void main(String[] args) {
        // String[]  split(String regex) 根据参数正则,将调用者字符串进行切割,将切割的每一段放进一个String[]
        String[] arr = "I hate java very much".split(" ");
        System.out.println(Arrays.toString(arr));
    
        String str = "人人为我,我为人人".replaceAll("人", "*******");
        System.out.println("str = " + str);
    }
    

    }
    `
    输出结果:

  • 相关阅读:
    实用C语言技巧
    ASP.NET的适配器设计模式(Adapter)
    MongoDB的管理
    WinForm实现类似QQ停靠,显示隐藏过程添加特效效果
    dreamhappy博客索引
    Namenode的介绍
    asp.net
    学习
    采用Mono进行移动开发图书推荐
    VS2010+C#写的3D
  • 原文地址:https://www.cnblogs.com/conglingkaishi/p/15017642.html
Copyright © 2020-2023  润新知