Java正则

总结了一下java正则的经常使用规则,具体以下java

一些概念:
一、正则中的各种特殊符号。包括限定符、非打印字符、定位符、元字符,它们的区别见TestCase
二、JAVA正则的API使用
    经常使用的方式是以下结构
     Pattern pattern = Pattern.compile(正则表达式);
  //得到Matcher对象
  Matcher matcher = pattern.matcher(目标字符串);
  //返回匹配的结果 boolean类型,匹配返回true、不匹配返回false
  boolean result = matcher.matches();
  //得到匹配的内容
  matcher.group(1)
 
 
TestCase:
import org.junit.Assert;
import org.junit.Test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @ProjectName: study
 * @Package: com.wt.study
 * @Description:
 * @Author: lichking2017@aliyun.com
 * @CreateDate: 2018/5/31 上午9:02
 * @Version: v1.0
 */
public class TestPattern {

    /**
     * 整体说明
     * 一、正则表达式、普通字符串,都是以字符串形式出现的。
     * 二、对于正则表达式中,一些须要加\的状况
     * 如非打印字符 \n \r
     * 如特殊字符的转义\(
     * 是都须要加上\\的 ,如\\n,由于\自己也须要使用\转义
     * 不然编译是不经过的
     * 三、而对于普通字符串
     * 非打印字符是直接可使用的\n,表明后续的字符串要换行
     * 特殊字符的转义也是要加\\的
     */

    private Pattern pattern;


    //▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽限定符测试 begin▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽
    @Test
    public void test1() {
        //限定符+,表明前面的字符至少出现一次
        pattern = Pattern.compile("runoo+b");
        //matcher 目标字符串是否匹配正则,匹配返回true,不然返回false
        Assert.assertTrue(pattern.matcher("runoooob").matches());//匹配
        Assert.assertFalse(pattern.matcher("runob").matches());//不匹配,少了一个o
        Assert.assertFalse(pattern.matcher("runoobc").matches());//不匹配 末尾多了一个c
        Assert.assertFalse(pattern.matcher("crunooob").matches());//不匹配 开头多了一个c
    }

    @Test
    public void test2() {
        //限定符*,表明前面的字符出现0次或者>=1次
        pattern = Pattern.compile("runoo*b");
        Assert.assertTrue(pattern.matcher("runob").matches());//匹配
        Assert.assertTrue(pattern.matcher("runooob").matches());//匹配

    }

    @Test
    public void test3() {
        //限定符?,表明前面的字符出现0次 或 1次
        pattern = Pattern.compile("colou?r");
        Assert.assertTrue(pattern.matcher("colour").matches());//匹配
        Assert.assertTrue(pattern.matcher("color").matches());//匹配
        Assert.assertFalse(pattern.matcher("colouur").matches());//不匹配,多了一个u
    }

    @Test
    public void test4(){
        //限定符{n},n 是一个非负整数。匹配前面的字符 n 次。
        pattern = Pattern.compile("colou{2}");
        Assert.assertTrue(pattern.matcher("colouu").matches());//匹配
        Assert.assertFalse(pattern.matcher("colouuu").matches());//不匹配,多了一个u
        Assert.assertFalse(pattern.matcher("colou").matches());//不匹配,少了一个u

        //限定符{n,m},m 和 n 均为非负整数,其中n <= m。最少匹配 n 次且最多匹配 m 次。
        pattern = Pattern.compile("colou{2,5}");
        Assert.assertTrue(pattern.matcher("colouu").matches());//匹配
        Assert.assertFalse(pattern.matcher("colouuuuuu").matches());//不匹配,多了一个u
        Assert.assertFalse(pattern.matcher("colou").matches());//不匹配,少了一个u

    }


    //▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽非打印字符测试 begin▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽
    @Test
    public void test5() {
        //非打印字符\n,匹配换行
        //相似的还有,\r匹配回车 \s匹配空格、制表符
        pattern = Pattern.compile("china\\nbeijing");
        Assert.assertTrue(pattern.matcher("china\nbeijing").matches());//匹配
        Assert.assertFalse(pattern.matcher("chinabeijing").matches());//不匹配,没有换行符
    }


    //▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽定位符测试 begin▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽
    @Test
    public void test6(){
        //定位符^,定位开始规则
        //定位符$,定位结尾规则
        //元字符\d,表示匹配数字
        pattern = Pattern.compile("^\\d{5}\\.com$");
        Assert.assertTrue(pattern.matcher("12345.com").matches());//匹配
        Assert.assertFalse(pattern.matcher("123.com").matches());//不匹配,数字只有三位,小于5
        Assert.assertFalse(pattern.matcher("abc.com").matches());//不匹配,不是以5位数字开头
        Assert.assertFalse(pattern.matcher("12345.co").matches());//不匹配,不是以.com结尾
    }
    @Test
    public void test7(){
        pattern = Pattern.compile("le\\b");
        Assert.assertTrue(pattern.matcher("maile").matches());
        Assert.assertFalse(pattern.matcher("erlen").matches());
    }
    @Test
    public void test100() {
        //一、注意\\的使用,转义为普通的字符。避免与正则的特殊字符冲突
        //二、注意组的使用(),使用后,能够在匹配后的matcher中得到匹配的具体内容
        //三、$表明之前面的字符串结尾,以.com结尾
        //四、只有matcher执行了find()方法或者matches()方法,才能获取具体匹配的组内容
        //五、\w表明匹配字母、数字和下划线
        pattern = Pattern.compile("\\((\\w+)\\)\\.com$");
        Matcher matcher = pattern.matcher("(wwwwt_123).com");
        //执行查找动做
        Assert.assertTrue(matcher.matches());//匹配
        //获取匹配的内容
        String target = matcher.group(1);
        System.out.println(target);  //输出 wwwwt_123
    }
}
相关文章
相关标签/搜索