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
    }
}
相關文章
相關標籤/搜索