总结了一下java正则的常用规则,具体如下
一些概念:
1、正则中的各类特殊符号。包括限定符、非打印字符、定位符、元字符,它们的区别见TestCase
2、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 {
/**
* 总体说明
* 1、正则表达式、普通字符串,都是以字符串形式出现的。
* 2、对于正则表达式中,一些需要加\的情况
* 如非打印字符 \n \r
* 如特殊字符的转义\(
* 是都需要加上\\的 ,如\\n,因为\本身也需要使用\转义
* 否则编译是不通过的
* 3、而对于普通字符串
* 非打印字符是直接可以使用的\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() {
//1、注意\\的使用,转义为普通的字符。避免与正则的特殊字符冲突
//2、注意组的使用(),使用后,可以在匹配后的matcher中获得匹配的具体内容
//3、$代表以前面的字符串结尾,以.com结尾
//4、只有matcher执行了find()方法或者matches()方法,才能获取具体匹配的组内容
//5、\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
}
}