Java正则表达式

一、Pattern与Matcher的使用

Pattern类的作用在于编译正则表达式后创建一个匹配模式

Matcher类使用Pattern实例提供的模式信息对正则表达式进行匹配

@Test
public void test01() {
    boolean isMatcher = Pattern.matches("\\d", "1");
    System.out.println(isMatcher);  
    //结果: true
}

@Test
public void test02() {
    // 表达式对象
    Pattern pattern = Pattern.compile("\\d");
    // 创建Matcher对象
    Matcher matcher = pattern.matcher("1");
    // 尝试将整个字符串序列与该模式匹配
    boolean isMatcher = matcher.matches();

    System.out.println(isMatcher);  
    //结果: true
}

@Test
public void test03() {
    // 表达式对象
    Pattern p = Pattern.compile("\\w+");
    // 创建Matcher对象
    Matcher m = p.matcher("ab&&cd");
    while (m.find()) {
        System.out.println(m.group());
    }
    // 结果: ab cd
}

二、正则表达式

1.标准字符集合

  • \d 任意一个数字,0~9中的任意一个
  • \w 任意一个字母或数字或下划线,也就是A~Z,a~z,0~9,_中任意一个
  • \s 包括空格、制表符、换行符等空白字符的其中任意一个
  • .小数点可以匹配任意一个字符,

能够与‘多种字符’匹配的表达式

注意区分大小写,大写是相反的意思

如果要匹配包括"\n"在内的所有字符,一般用[\s\S]

@Test
public void test11() {
    boolean b1 = Pattern.matches("\\d", "1");   // true
    boolean b2 = Pattern.matches("\\w", "s");   // true
    boolean b3 = Pattern.matches("\\s", " ");   // true
    boolean b4 = Pattern.matches(".", "a");     // true
}

2.自定义字符集合

  • [ ] 包含多个字符,能够匹配方括号中任意一个字符
  • [^ ] 包含多个字符,构成否定格式,可以匹配所包含的字符之外的任意一个字符

标准字符集合,除小数点外,如果被包含于中括号,自定义字符集和将包含该集合

@Test
public void test12() {
    boolean b1 = Pattern.matches("[abc]", "a");  // true
    boolean b2 = Pattern.matches("[^abc]", "1"); // true
    boolean b3 = Pattern.matches("[\\d]", "2");  // true
    boolean b4 = Pattern.matches("[.]", ".");    // true 只能匹配"."
}

3.量词

  • {n} 表达式重复n次
  • {m,n} 表达式至少重复m次,最多重复n次
  • {m,} 表达式至少重复m次
  • ? 匹配表达式0次或者1次,相当于{0,1}
  • + 表达式至少出现1次,相当于{1,}
  • * 表达式不出现或者出现任意次,相当于{0,}

匹配次数中的贪婪模式(匹配字符越多越好,默认)
匹配次数中的非贪婪模式(匹配字符越少越好,修饰匹配次数的特殊符号后再加上一个"?"号)

@Test
public void test13() {
    boolean b1 = Pattern.matches("\\d{3,6}", "1234");  // true
}

4.字符边界

  • ^ 与字符串开始的地方匹配
  • $ 与字符串结束的地方匹配
  • \b 匹配一个单词边界,匹配这样一个位置:前面的字符和后面的字符不全是\w

匹配的不是字符而是位置,符合某种条件的位置

@Test
public void test14() {
   boolean b1 = Pattern.matches("^\\d+w$", "1w");  // true

   Pattern p = Pattern.compile("abc\\b");
   Matcher m = p.matcher("abcd abc b");
   while (m.find()) {
       System.out.println(m.group());
   }
   // abc
}

5.选择符与捕获组

  • | 分支结构 左右两边表达式之间"或"的关系,匹配左边或者右边
  • () 捕获组
  • (?:Expression) 非捕获组 一些表达式中,不得不使用(),但又不需要保存()中子表达式匹配的内容,这时可以用非捕获组来抵消使用()带来的副作用

捕获组:

(1) 在被修饰匹配次数的时候,括号中的表达式可以作为整体被修饰

(2) 取匹配结果的时候,括号中的表达式匹配到的内容可以被单独得到

(3) 每一对括号会分配一个编号,使用()的捕获根据左括号的顺序从1开始自动编号

@Test
public void test15() {
    Pattern p1 = Pattern.compile("((a)(bcd))");
    Matcher m1 = p1.matcher("abcdefabcdef");
    while (m1.find()) {
        System.out.print(m1.groupCount() + " " + m1.group() + " " + m1.group(0) + " ");
        System.out.print(m1.group(1) + " " + m1.group(2) + " " + m1.group(3));
        System.out.println();
    }
    // 3 abcd abcd abcd ab cd
    // 3 abcd abcd abcd ab cd

    Pattern p2 = Pattern.compile("((?:a)(bcd))");
    Matcher m2 = p2.matcher("abcdefabcdef");
    while (m2.find()) {
        System.out.print(m2.groupCount() + " " + m2.group() + " " + m2.group(0) + " ");
        System.out.print(m2.group(1) + " " + m2.group(2));
        System.out.println();
    }
    // 2 abcd abcd abcd bcd
    // 2 abcd abcd abcd bcd
}

6.反向引用(\nnn)

  • 每一对()会分配一个编号,使用()的捕获根据左括号的顺序从1开始自动编号
  • 通过反向引用,可以对分组已捕获的字符串进行引用
@Test
public void test16() {
    boolean b = Pattern.matches("(\\w{2})\\1", "abab"); // true
}

7.预搜索

  • (?=exp) 断言自身出现的位置的后面能匹配表达式exp
  • (?<=exp) 断言自身出现的位置的前面能匹配表达式exp
  • (?!exp) 断言此位置的后面不能匹配表达式exp
  • (?<!exp) 断言此位置的前面不能匹配表达式exp

只进行子表达式的匹配,匹配内容不计入最终的匹配结果,是零宽度

正则表达式匹配过程中,如果子表达式匹配到的是字符内容,而非位置,并被保存到最终的匹配结果中,那么就认为这个子表达式是占有字符的;如果子表达式匹配的仅仅是位置,或者匹配的内容并不保存到最终的匹配结果中,那么就任认为这个子表达式零宽度的。占有字符还是零宽度,是针对匹配的内容是否保存到最终的匹配结果中而言的

@Test
public void test17() {
   Pattern p = Pattern.compile("[a-z]+(?=ing)");
   Matcher m = p.matcher("eating walking say going");
   while (m.find()) {
       System.out.println(m.group());
   }
   // eat walk go
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值