一次性解决正则表达式

1.正则表达式

字符匹配 matches

		"Dog".matches("(?i)dog");	// 忽略大小写 : true

		"我爱中国\\中国爱我\\".split("\\\\"); // [我爱中国, 中国爱我]

获得匹配器 Macher

字符

// [abc] {a或b或c 单个字符}
// [^abc] {非 a和b和c 的单个字符 : 补集}
// [a-z] {a到z 的任意单个字符都行}
// [a-c[e-f]] {a-c 或 e-f 单个字符 : 并集}
// [a-c&&[c-f]] {a-c 且 c-f 单个字符 : 交集}


// true
"T".matches("[\tT]"
// true            
"a".matches("[^\tT]"
// true
"a".matches("[a-c]"
// true            
"a".matches("[a-c[e-g]]
// true            
"c".matches("[a-c&&[c-f]]")         

预定义字符

// . {排除 \r 和 \n 的 单个字符 : 补集 - 不要写在 [] 里面}
// \\w {[a-zA-Z0-9_]}
// \\W {[^a-zA-Z0-9_]}
// \\s {[\u000b\n\r\t\f]}
// \\S {非空白}
// \\d {[0-9]}
// \\D {[^0-9]}

// false            
"\r".matches(".")
"\n".matches(".")  

    
    
    
    
    
// false    
"罗".matches("\\w");
// true
"_".matches("\\w");
"a".matches("\\w");
"1".matches("\\w");

// true
"\t".matches("\\s");

// true
"1".matches("\\d");

数量表示

表达式描述
?匹配前面的表达式0个或1个。即表示可选项。
+匹配前面的表达式至少1个。
*匹配前面的表达式0个或多个。
``
{m}匹配前面的表达式m个。
{m,}匹配前面的表达式最少m个。
{m,n}匹配前面的表达式最少m个,最多n个。
        // false
        System.out.println("".matches("\\d+"));
        // true
        System.out.println("".matches("\\d+|\\d?"));
        // true
        System.out.println("12".matches("\\d{1}|\\d{1,2}"));
        // 贪婪量词 : 吞下字符串, 从后面找到符合 foo的部分
        // 前面 xfooxxxxxx 符合 .* 返回 xfooxxxxxxfoo -> ORZ -> ORZ1
        "xfooxxxxxxfoo1".replaceAll(".*foo", "ORZ");

        // 逐步量词 : 找到 xfoo 符合 .*?foo
        // 剩下的部分 xxxxxxfoo 符合 .*?foo
        // xfoo -> ORZ xxxxxxfoo -> ORZ -> ORZORZ1
        "xfooxxxxxxfoo1".replaceAll(".*?foo", "ORZ");

        // 独吞量词 : 吞下字符串 发现符合 .*+ 
        // 没有剩余部分匹配 foo 所以无事发生 -> xfooxxxxxxfoo1
        "xfooxxxxxxfoo1".replaceAll(".*+foo", "ORZ");

边界

表达式描述
^匹配字符串或行开头。
$匹配字符串或行结尾。
\b匹配单词边界。比如Sheep\b可以匹配CodeSheep末尾的Sheep,不能匹配CodeSheepCode中的Sheep
\B匹配非单词边界。比如Code\B可以匹配HelloCodeSheep中的Code,不能匹配HelloCode中的Code
        // [justin ,  monica doggie Irendog]
        // 设置单词边界
        "justin dog monica doggie Irendog".split("\\bdog\\b");

        // [justin ,  monica  ,  dog ,  gie]
        // 设置单词边界
        "justin 罗 monica  罗 dog 罗 gie".split("\\b罗\\b");

        // [justin ,  monica  ,  dog ,  gie]
        // 设置单词边界
        "justin _ monica  _ dog _ gie".split("\\b_\\b");

        // [justin ,  monica  ,  dog ,  gie]
        // 设置单词边界
        "justin 1 monica  1 dog 1 gie".split("\\b1\\b");

        // [justin ,  monica  ,  dog ,  gie]
        // 非单词边界
        "justin \t monica  \t dog \t gie".split("\\B\\t\\B");

分组和引用

表达式描述
(expression)分组。匹配括号里的整个表达式。
(?:expression)非捕获分组。匹配括号里的整个字符串但不获取匹配结果,拿不到分组引用。
\num对前面所匹配分组的引用。比如(\d)\1可以匹配两个相同的数字,(Code)(Sheep)\1\2则可以匹配CodeSheepCodeSheep
        // true
        "1111".matches("^(\\d*)|(\\D*)$");

        // true
        "wwww".matches("^((\\d\\d)|(\\D\\D))\\1$");

        // true 第一个分组是非捕获分组, 不算
        "1111".matches("^(?:(\\d\\d)|(\\D\\D))\\1$");

预查断言

表达式描述
(?=)正向预查。比如Code(?=Sheep)能匹配CodeSheep中的Code,但不能匹配CodePig中的Code
(?!)正向否定预查。比如Code(?!Sheep)不能匹配CodeSheep中的Code,但能匹配CodePig中的Code
(?<=)反向预查。比如(?<=Code)Sheep能匹配CodeSheep中的Sheep,但不能匹配ReadSheep中的Sheep
(?<!)反向否定预查。比如(?<!Code)Sheep不能匹配CodeSheep中的Sheep,但能匹配ReadSheep中的Sheep
        // [Hello, World!!]
        "HellosplitWorld!!".split("split(?=World)");

        // [Hello, world!!]
        "Hellosplitworld!!".split("split(?!World)");

        // [hello, world!!]
        "hellosplitworld!!!".split("(?<=hello)split");

        // [Hello, world!!]
        "Hellosplitworld!!!".split("(?<!hello)split");

        // true
        "dogabcabc".matches("((?i)dog([a][b][c]))\\2");

忽略大小写

		"Dog".matches("(?i)dog");	// 忽略大小写 : true

		"我爱中国\\中国爱我\\".split("\\\\"); // [我爱中国, 中国爱我]

2.Pattern

代表 regex 对象

		// 获得 Pattern 对象
		Pattern compile = Pattern.compile(".*foo");
		// 获得比较器对象
        Matcher matcher = compile.matcher("xfoox");
        // ture 开头符合即可
        matcher.lookingAt();
        //                             贪婪量词,  独吞量词,  逐步量词
        String[] regexs = new String[]{".*foo", ".*+foo", ".*?foo"};

        // .*foo find  in xfooxxxxxxxxxxxxxxfoo
        // .*+foo find
        // .*?foo find  in xfoo in xxxxxxxxxxxxxxfoo
        for (String regex : regexs) {
            Pattern compile = Pattern.compile(regex);
            Matcher matcher = compile.matcher("xfooxxxxxxxxxxxxxxfoo");
            // 查询是否有符合的内容
            while (matcher.find()) {
                // 拿到符合的内容
                matcher.group();
            }
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值