ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

2022-08-12 第五组 赖哲栋 学习笔记

2022-08-12 19:32:32  阅读:149  来源: 互联网

标签:regex 12 匹配 String -- Pattern 08 第五组 字符串


正则表达式

  • 又叫规则表达式----判断字符串,核心功能处理文本

  • 正则表达式不局限于某一种语言

  • 元字符

    • .: --匹配除了换行符之外的任意字符
    • \w --匹配字符或数字或下划线或汉字
    • \s --空格
    • \d --匹配数字
    • \b --匹配单词的开始或结束
    • ^ --匹配字符串的开始
    • $ --匹配字符串的结束
  • 匹配8位数字:^\d\d\d\d\d\d\d\d$

  • 匹配1开头的11位数字:^1\d\d\d\d\d\d\d\d\d\d$

  • 重复限定符

      • -- 重复零次或更多次
      • --重复一次或更多次
    • ? --重复0次或1次
    • {n} --重复n次
    • {n,} --重复n次或更多次
    • {n,m} --重复n到m次
  • 匹配8位数字:^\d{8}$

  • 匹配1开头的11位数字:^1\d{10}$

  • 银行卡号14~18位:^\d{14,18}$

  • 匹配以a开头,0个或多个b结尾的字符串^ab*8$

  • 分组

  1. 限定符的作用与它相邻的最左边的一个字符起作用
  2. 如果想要ab同时被限定怎么办?----正则表达式中可以用小括号来分组,括号内的内容会作为一个整体--^(ab)*$
  • 转义

    • 匹配字符串中包含0到多个(ab)开头---^((\ab))*
  • 区间

    • 正则表达式提供了一个[]来表示区间
    • 0~9:[0-9]
    • A~Z:[A-Z]
    • 限定某些数字:[130]
    • 单或|----^((13[0-2])|(15[5-6]))\d{8}$----130、131、132、155、156开头的8位数字
  • 反义

    • \W:不是字母。数字、下划线、汉字
    • \S:不是空格
    • \D:不是数字
    • \B:不是单词开头或结束
    • [^X]:除了X以外的任意字符
    • [^aeiou]:匹配除了aeiou的任意字符
  • 常见的正则表达式

- 匹配中文的字符:[],匹配的是ASCII码
- 邮箱:^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$
- 国内的座机电话:^\d{3,4}-\d{8}$
- QQ号:^[1-9][0-9]{4,11}$
- 身份证号:^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$
  • 正则表达式相关的类:
    • Pattern类
    • Matcher类
    • PatternSyntaxException类
  • Pattern类方法
    • Pattern compile(String regex):regex正则表达式,该方法是将给定的正则表达式编译并赋予给Pattern类
    • Pattern.split() 将字符串从正则表达式匹配的地方分开
    • Pattern.matcher():对整个字符串进行匹配,若整个字符串都匹配,返回true;
  • Matcher类方法
    • find():对字符串进行匹配,匹配到的字符串可以在任何位置,若匹配到,返回true
    • matches(): 对整个字符串进行匹配,若整个字符串都匹配,返回true
    • replaceAll(String regex, String replacement):用replacement替换所有的regex匹配项,regex是正则表达式,replacement是字符串;
    //split()将字符串从正则表达式匹配的地方分开
    public void test06() {
        String regex = "[-_]";
        String str = "123-4756_qweqwe-7987_465";
        String[] split = str.split(regex);
        System.out.println(Arrays.toString(split));
    }

    @Test
    public void test05() {
        String regex = "\\d";
        String str = "1111c2222d456456456f465gh987897";

        String s = str.replaceAll(regex,"@");
        System.out.println(s);
    }

    @Test
    public void test04() {
        String regex = "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$";
        String email = "1440542956@qq.com";
        System.out.println(email.matches(regex));
    }

    
    public void test03(){
        String regex = "cat";
        String str = "cat cat dog dog cat";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);

        //统计cat在字符串中出现的次数
        int count = 0;
        while(matcher.find()){
            count++;
        }
        System.out.println("出现了" + count + "次");
    }

    //compile(String regex)将给定的正则表达式编译赋予给Pattern类
    public void test02(){
        String regex = "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$";
        String email = "1440542956@qq.com";
        Pattern compile = Pattern.compile(regex);
        Matcher matcher = compile.matcher(email);
        System.out.println(matcher.matches());
    }
    
    //matcher():匹配字符串
    public void test01() {
        String str = "hello,i am from jilin changchun!";
        // 必须包含jilin
        String pattern = ".*jilina.*";
        boolean b = Pattern.matches(pattern,str);
        System.out.println("字符串中是否包含了jilin:" + b);
    }

标签:regex,12,匹配,String,--,Pattern,08,第五组,字符串
来源: https://www.cnblogs.com/laizhedong/p/16581097.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有