廖雪峰Java9正则表达式-1正则表达式入门-2正则表达式匹配规则
正则表达式的匹配规则:
- 从左到右按规则匹配
|匹配规则及示例|可以匹配 |不能匹配
|---------|-------|--------|----------|
|"abc"| "abc"| 不能匹配:"ab", "Abc", "abcd"
|特殊字符需要转义,如"a&c",注意Java的字符串需要再加一个转义符| "a&c" | "ab", "abc", "a&cd"
|非ascii字符用"\u####"表示一个unicode字符,如"a\u548cc"| "a和c"| "a与c","a&c","abc"
|. 可以匹配任意一个字符:"a.c" | "abc", "a&c" | "ac", "abbc"
|\d 可以匹配一个数字:"00\d"| "007", "008"| "00A", "0077"
\D可以匹配一个非数字:"00\D" | "00A", "00#" | "001"
\w可以匹配一个字母、数字或下划线:"java\w"| "javac", "java8", "java_",| "java#", "java "
\s可以匹配一个空白字符:"A\sB"| "A B" "A B",tab键也算空白字符 | "AB" "A B"
\W可以匹配一个非字母、数字或下划线:"java\W"| "java!", "java "| "javac", "java8", "java_"
\S可以匹配一个非空白字符:"A\SB" | "A&B", "ABB"| "A B","A B"tab键也算空白字符
|修饰符可以匹配前的任意个字符:"A\d*",先匹配字母A,再匹配任意多个数字| "A", "A1", "A007" | "AB"
|修饰符+可以匹配+前的至少一个字符:"A\d+",先匹配字母A,再匹配至少1个数字| "A1","A007"| "A", "AB"
|修饰符?可以匹配0个或1个字符:"A\d?",先匹配字母A,再匹配0个或1个数字 | "A","A1"| "A12","AB"
|修饰符{n}可以匹配n个字符:"\d{6}"| "123456", "999999"| "12345","1234567", "12345A"
|修饰符{m,n},可以匹配m-n个字符"\d{3,5}"| "123", "12345"| "12","12345", "123A5"
|修饰符{n,}可以匹配至少n个字符:"\d{3,}" | "123", "1234", "123456789"| "12","123A"
|^和\(匹配开头和结尾:"^A\d{3}\)"|"A001", "A999"| "B001", "A0001"
|[...]可以匹配范围内的字符: "[abc]1"| "a1", "b1", "c1"| "ab1", "x1"
| "[a-f]1"| "a1", "b1", "f1"| "g1", "ab1"
|"[a-f0-9]{6}"| "1a2b3c", "ffffff", "ff0102" | "abc12", "A0000F"
[^...] 在中括号内表示非,"[0-9]{6} 6位非数字字符串| "abcdef", "$$$$$$", "A-B-C-" | "1a2b3c", "A0000F"
|AB|CD可以匹配AB或CD;如"java|php" | "java", "php" | "C++", "jphp"
|"learn\s(java|php)" | "learn java", "learn php" | "learn C++", "learn python"
|"learn\sjava|php" | "learn java", "php" | "learn java", "learn php"
package com.testRegex;
public class Telephone {
public static Boolean invaildTel(String s) {
return s.matches("\\d{3,4}\\-\\d{6,8}");
}
}
package com.testRegex;
import static org.junit.Assert.*;
import org.junit.Test;
public class TelephoneTest {
@Test
public void testInvaildTel() {
assertTrue(Telephone.invaildTel("010-123456"));
assertTrue(Telephone.invaildTel("010-12345678"));
assertTrue(Telephone.invaildTel("0123-123456"));
assertTrue(Telephone.invaildTel("0123-12345678"));
assertTrue(Telephone.invaildTel("123-123456"));
assertTrue(Telephone.invaildTel("123-12345678"));
assertFalse(Telephone.invaildTel("010#123456"));
assertFalse(Telephone.invaildTel("010X12345678"));
assertFalse(Telephone.invaildTel("01-12345678"));
assertFalse(Telephone.invaildTel("01234-12345678"));
assertFalse(Telephone.invaildTel("01A-12345678"));
assertFalse(Telephone.invaildTel("0123-12345"));
assertFalse(Telephone.invaildTel("0123-12345X"));
assertFalse(Telephone.invaildTel("0123-123456789"));
assertFalse(Telephone.invaildTel("0123-12345678X"));
}
}
![](https://img2018.cnblogs.com/blog/1418970/201904/1418970-20190422163405171-586066461.png)
![](https://img2018.cnblogs.com/blog/1418970/201904/1418970-20190422202358363-296780882.png)
匹配QQ号,非0开头的5到11位数字字符串
package com.testList;
public class QQ {
public static Boolean isVaildQQ(String s){
return s.matches("^[1-9]\\d{4,10}$");
}
}
package com.testList;
import org.junit.Test;
import static org.junit.Assert.*;
public class QQTest {
@Test
public void isVaildQQ() {
assertTrue(QQ.isVaildQQ("10000"));
assertTrue(QQ.isVaildQQ("99999"));
assertTrue(QQ.isVaildQQ("1234567890"));
assertTrue(QQ.isVaildQQ("9999999999"));
assertFalse(QQ.isVaildQQ("00001"));
assertFalse(QQ.isVaildQQ("099999"));
}
}
电话号码优化,区号必须以0开头,电话号码不能以0开头
package com.testList;
public class Phone{
public static boolean isVaildPhone(String s){
return s.matches("^0\\d{2,3}(\\-|\\s)[1-9]\\d{6,7}$");
}
}
package com.testList;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
public class PhoneTest {
@Test
public void testIsVaildPhone(){
assertTrue(Phone.isVaildPhone("012-12345678"));
assertTrue(Phone.isVaildPhone("012 12345678"));
assertTrue(Phone.isVaildPhone("0123 1234567"));
assertTrue(Phone.isVaildPhone("0123-1234567"));
assertTrue(Phone.isVaildPhone("012 12345678"));
assertFalse(Phone.isVaildPhone("123-12345678"));
assertFalse(Phone.isVaildPhone("123-0123456"));
assertFalse(Phone.isVaildPhone("023-0123456"));
assertFalse(Phone.isVaildPhone("023-01234567"));
}
}