java正则表达式

概述

正则表达式:由一对字符和符号组成。作用是找出字符串中符合格式的文本,将它转换成我们想要的样子。例如:将字符串str = "Hello world 123"中的所有数字删除,

str.replaceAll("//d",""); //str :Hello world

参数列表

.

代表任意字符

[abc]

字符集合

[a-z]、[0-9]、[A-Z]

范围

[^abc] [^a-z]

除abc外的所有字符,所有非小写字母

*

在字符或表达式后边加上*,代表字符不出现,出现一次或者出现多次

+

在字符或表达式后加上+,代表字符出现一次或者多次

在字符或表达式后加上?,代表字符可有可无

{n}

在字符或表达式后加上{n},代表字符出现n次

{n,}

在字符或表达式后加上{n,},代表字符最少出现n次

{n,m}

在字符或表达式后加上{n,m}代表字符出现n到m次

(),(ha)-\\1,(haa)-\\2

分组:将ha作为第一组,haa作为第二组,可以通过函数调用

(?:)

非捕获分组,不做为正式的分组

\

转义字符

^ ,^[a-z]

在开头的字母

$, html$

在末尾的html

\w

代表数字、字母、下划线

\W

非数字、字母、下划线

\d

数字

\D

非数字

\s

空格

\S

非空格

(?i) , (?i)a

不区分(?i)后内容的大小写,

(?=) 正向先行断言

\d(?=PM) PM前的数字

(?!) 负向先行断言

\d(?!PM) 不再PM之前的数字

(?<=) 正向后行断言

(?<=$)\d $后的数字

(?<!) 负向后行断言

(?<!$)\d 不再$后的数字

代码练习

//正则表达式练习
        String str = "“I have no special talents. I am only passionately curious.”";
        //基本匹配,查找curious单词,删除掉
        str = str.replaceAll("curious","");
        System.out.println("查找curious单词,删除掉:"+str);
        //.代表任意字符
        str = "“I have no special talents. I am only passionately curious.”";
        str = str.replaceAll(".","");
        System.out.println(".代表任意字符:"+str);
        //[abc]字符集合
        str = "“I have no special talents. I am only passionately curious.”";
        str = str.replaceAll("[aih]","");
        System.out.println("[abc]代表所有的a,b,c字符:"+str);
        //[a-z]字母范围(小写)[0-9]数字范围
        str = "“I have no special talents.123 I am only passionately curious.”";
        str = str.replaceAll("[a-z0-9]","");
        System.out.println("[a-z0-9]]:"+str);
        //[^abc]代表所有非abc的字符
        str = "“I have no special talents. I am only passionately curious.”";
        str = str.replaceAll("[^abc]","");
        System.out.println("[^abc]:"+str);
        //[^a-z]代表所有 非小写的字母
        str = "“I have no special talents. I am only passionately curious.”";
        str = str.replaceAll("[^a-z]","");
        System.out.println("[^a-z]:"+str);
        //重复:*,+,?
        //*,在字符或表达式后边加上*,代表这个字符不出现,出现一次,出现多次
        str = "abc,abbc,ac,Abbc";
        str = str.replaceAll("ab*c","");
        System.out.println("字符*:"+str);
        //+,在字符或表达式后加上+,代表这个字符出现一次或多次
        str = "abc,abbc,ac,Abbc";
        str = str.replaceAll("ab+c","");
        System.out.println("字符+:"+str);
        //?,在字符或表达式后加上?,代表这个字符可有可无
        str = "abc,abbc,ac,Abbc";
        str = str.replaceAll("ab?c","");
        System.out.println("字符?:"+str);
        //{n},字符或表达式后加上{n}代表字符出现n次
        str = "abc,abbc,ac,Abbc";
        str = str.replaceAll("ab{2}c","");
        System.out.println("字符{n}:"+str);
        //{n,},字符或表达式后加上{n,}代表字符最少出现n次
        str = "abc,abbc,ac,abbbc";
        str = str.replaceAll("ab{2,}c","");
        System.out.println("字符{n,}:"+str);
        //{n,m},字符或表达式后加上{n,m}代表字符出现n到m次
        str = "abc,abbc,ac,abbbc";
        str = str.replaceAll("ab{1,3}c","");
        System.out.println("字符{n,m}:"+str);
        str = "Release 10/9/2021";
        str = str.replaceAll("[0-9]{1,3}","A");
        System.out.println("字符{n,m}:"+str);

        //()分组
        str = "ha-ha,haa-haa";
        System.out.println("()分组:"+str.replaceAll("(haa)","A"));
        // 按指定模式在字符串查找
        String line = "ha-ha,haa-haa";
        String pattern = "(ha)-\\1,(haa)-\\2";
        //(?:) 非捕获分组,下边有两个分组,第一个(?:ha)-ha 第二个(haa),
        //由于第一个分组有?:修饰,所以第一个分组不会捕获
        //  只有(haa)一个分组。
        //String pattern = "(?:ha)-ha,(haa)-\\1";
        // 创建 Pattern 对象
        Pattern r = Pattern.compile(pattern);
        // 现在创建 matcher 对象
        Matcher m = r.matcher(line);
        if (m.find()) {
            //m.group(0)代标原值
            System.out.println("Found value: " + m.group(0) );
            System.out.println("Found value: " + m.group(1) );
            System.out.println("Found value: " + m.group(2) );
        } else {
            System.out.println("NO MATCH");
        }
        //|竖线 或
        str = "cat,rat,dog";
        str = str.replaceAll("(c|r)at|dog","A");
        System.out.println("| :"+str);
        //转义字符:java中正则表达式转义字符式 \\=》\
        str = "(*),Asterisk.";
        str = str.replaceAll("\\*|\\.","A");
        System.out.println("转义字符\\:"+str);

        // 字符开头:^ 查找字符串开头的数字
        str = "1aaasdsdf";
        str = str.replaceAll("^[0-9]","A");
        System.out.println("字符开头^:"+str);
        // 字符结尾:$  查找字符串末尾为html的
        str = "wwww.asda.html";
        str = str.replaceAll("html$","A");
        System.out.println("字符结尾 $:"+str);

        // \w代表数字、字母、下划线。将字母数字下划线替换为A
        str = "abcABC123 _.:!?";
        str = str.replaceAll("\\w","A");
        System.out.println("\\w:" +str);
        // \W匹配除数字、字母、下划线的字符。=[^\w]
        str = "abcABC123 _.:!?";
        System.out.println("\\W:" +str.replaceAll("\\W","A"));
        System.out.println("[^\\w]:" +str.replaceAll("[^\\w]","A"));
        //  \d:用来匹配数字  \D:非数字
        System.out.println("\\d:" +str.replaceAll("\\d","A"));
        System.out.println("\\D:" +str.replaceAll("\\D","A"));
        // \s:空白符  \S:非空白符
        System.out.println("\\s:"+str+"-"+str.replaceAll("\\s","A"));
        System.out.println("\\S:"+str+"-"+str.replaceAll("\\S","A"));
        // (?i) 不区分带小写 (?i)后的字母不区分大小写
        str = "AbcBcd";
        System.out.println("(?i) "+str+":"+str.replaceAll("(?i)A","1"));
        System.out.println("(?i) "+str+":"+str.replaceAll("(?i)Bc","1"));

        //如果我们希望特定词出现在另一个词语之前或之后
        //正向先行断言,负向先行断言
        //正向后行断言,负向后行断言
        //例如,我们要匹配文本中的小时值。为了只匹配后面有 PM 的数值,
        // 我们需要在表达式后面使用正向先行断言 (?=),并在括号内的 = 后面添加 PM。
        str = "Date: 4 Aug 3PM";
        System.out.println("(?=) "+str+":"+str.replaceAll("\\d(?=PM)","A"));
        //例如,我们要在文本中匹配除小时值以外的数字。我们需要在表达式后面使用负向先行断言 (?!),
        // 并在括号内的 ! 后面添加 PM,从而只匹配没有 PM 的数值。
        System.out.println("(?!) "+str+":"+str.replaceAll("\\d(?!PM)","A"));
        //例如,我们要匹配文本中的金额数。为了只匹配前面带有 $ 的数字。
        // 我们要在表达式前面使用正向后行断言 (?<=),并在括号内的 = 后面添加 \$。
        str = "Product Code: 1064 Price: $5";
        System.out.println("(?<=) "+str+":"+str.replaceAll("(?<=\\$)\\d","A"));
        //例如,我们要在文本中匹配除价格外的数字。
        // 为了只匹配前面没有 $ 的数字,我们要在表达式前用负向后行断言 (?<!),
        //并在括号内的 ! 后面添加 \$。
        System.out.println("(?<!) "+str+":"+str.replaceAll("(?<!\\$)\\d","A"));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值