正則表達式匹配常用字符(只含常用):
字符:
x | 字符x |
\n | 新行(換行)符 ('\u000A') |
\t | 製表符 ('\u0009') |
字符類:
[abc] | a、b 或 c(簡單類) |
[^abc] | 任何字符,除了 a、b 或 c(否定) |
[a-zA-Z] | a 到 z 或 A 到 Z,兩頭的字母包括在內(範圍) |
[a-d[m-p]] | a 到 d 或 m 到 p:[a-dm-p](並集) |
[a-z&&[def]] | d、e 或 f(交集) |
[a-z&&[^bc]] | a 到 z,除了 b 和 c:[ad-z](減去) |
[a-z&&[^m-p]] | a 到 z,而非 m 到 p:[a-lq-z](減去) |
預定義字符類:
. | 任何字符(與行結束符可能匹配也可能不匹配) |
\d | 數字:[0-9] |
\D | 非數字: [^0-9] |
\s | 空白字符:[ \t\n\x0B\f\r] |
\S | 非空白字符:[^\s] |
\w | 單詞字符:[a-zA-Z_0-9] |
\W | 非單詞字符:[^\w] |
邊界匹配器:
\b | 單詞邊界 |
\B | 非單詞邊界 |
Greedy數量詞:
X? | X,一次或一次也沒有 |
X* | X,零次或多次 |
X+ | X,一次或多次 |
X{n} | X,恰好 n 次 |
X{n,} | X,至少 n 次 |
X{n,m} | X,至少 n 次,但是不超過 m 次 |
例子:檢查QQ號
public class Main {
public static void main(String args[]) {
String QQ="a1234";
//[1-9]第一位在1-9中間,[0-9]第二位在0-9之間,{4,14}代表0-9重複4~14遍
//String.matches(regex);判斷字符串是否符合正則表達式
//等價於boolean b=QQ.matches("[1-9]\\d{4,14}");
boolean b=QQ.matches("[1-9][0-9]{4,14}");
System.out.println(b);
}
}
public class Main {
public static void main(String args[]) {
String QQ="a1234";
boolean b=QQ.matches("a[1-9]\\d+");//+代表1-9出現1次或多次 true
System.out.println(b);
b=QQ.matches("a[1-9]\\d?");//+代表1-9出現0次或一次 false
System.out.println(b);
b=QQ.matches("a[1-9]\\d*");//+代表1-9出現0次或多次 true
System.out.println(b);
}
}
正則表達式的四項功能:
1、匹配
例子:校驗手機號是否正確
public class Main {
public static void main(String args[]) {
String tel="15800001111";
//第一位1,第二位3或者5或者8,\\d{9}代表0-9出現了9次
String regex="1[358]\\d{9}";
boolean b=tel.matches(regex);
System.out.println(b);//true
}
}
2、切割
例子:名字切割(一個或多個空格)
public class Main {
public static void main(String args[]) {
String temp="zhangsan lisi wangwu";//中間多個空間,不能使用split(" ")切割
String regex=" +";
String[] names=temp.split(regex);
for (String name : names) {
System.out.println(name);
}
}
}
例子:用.(點)切割
public class Main {
public static void main(String args[]) {
String temp="zhangsan.lisi.wangwu";
//.在正則表達式代表任意字符,有特殊含義,需要轉義才能用.分割
String regex="\\.";
String[] names=temp.split(regex);
for (String name : names) {
System.out.println(name);
}
}
}
重點:捕獲組的概念
小括號括起來叫一個組,系統自動給序號,第x個左括號是第x組,組的下標從1開始,0是特殊組,代表整個表達式,比如在本字符串中調用第1組需要使用\\1,在其他的字符串中調用第1組需要使用$1
編號例子:在表達式((A)(B(C)))中有四組:
1 | ((A)(B(C))) |
2 | (A) |
3 | (B(C)) |
4 | (C) |
捕獲組例子:切割疊詞:
public class Main {
//運行結果que teg iou abc
public static void main(String args[]) {
String temp="que###teg$$$$$$$iouzzzzzzzzzzabc";
//(.)這是一個組,\\1調用這個組,代表本字符第2次出現,+代表一次或者多次
//也就是一個字符出現兩次或者兩次以上,以疊詞切割
String regex="(.)\\1+";
String[] names=temp.split(regex);
for (String name : names) {
System.out.println(name);
}
}
}
3、替換
例子:隱藏手機號
public class Main {
//運行結果159****1111
public static void main(String args[]) {
String temp="15900001111";
//利用捕獲組將手機號分爲3組,在第二個字符串中調用這兩個捕獲組
temp=temp.replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2");
System.out.println(temp);
}
}
例子:將疊詞變爲一個
public class Main {
//運行結果:qwe#tyg$iouzsdf
public static void main(String args[]) {
String temp="qwe##tyg$$$$$$iouzzzzzzzsdf";
temp=temp.replaceAll("(.)\\1+","$1");
System.out.println(temp);
}
}
4、獲取
Pattern類:正則表達式的編譯表示形式,通常使用其static Pattern compile(String regex)方法將給定的正則表達式編譯到模式中。例如:Pattern p=Pattern.compile(regex);將規則編譯成對象。
Matcher類:充當通過解釋Pattern對字符序列執行匹配操作的引擎。
例如:Matcher m=p.matcher("aaaaab");與要操作的字符串進行關聯,生成匹配器對象。
Matcher常用的三個方法:
boolean matches();嘗試整個區域與模式匹配
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String args[]) {
String temp = "aaa111";
String regex=".{6}";
//將規則編譯成Pattern對象
Pattern p = Pattern.compile(regex);
//和字符串關聯,獲取匹配器對象
Matcher m = p.matcher(temp);
//整體匹配,結果爲true
System.out.println(m.matches());
}
}
boolean lookingAt();嘗試將從區域開頭開始的輸入序列與該模式匹配。
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String args[]) {
String temp = "aaa111";
String regex=".{3}";
//將規則編譯成Pattern對象
Pattern p = Pattern.compile(regex);
//和字符串關聯,獲取匹配器對象
Matcher m = p.matcher(temp);
//開頭匹配,結果爲true
System.out.println(m.lookingAt());
}
}
boolean find();重置此匹配器,然後嘗試查找匹配該模式、從指定索引開始的輸入序列的下一個子序列。
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
//運行結果jia zhu jia
public static void main(String args[]) {
String temp = "da jia zhu yi le,ming tian fang jia le!";
//取出由三個字母組成的單詞
String regex = "\\b[a-zA-Z]{3}\\b";//\\b爲邊界
//將規則編譯成Pattern對象
Pattern p = Pattern.compile(regex);
//和字符串關聯,獲取匹配器對象
Matcher m = p.matcher(temp);
while(m.find()){
System.out.println(m.group());
}
}
}
int start();返回以前匹配的初始索引。
int end();返回最後匹配字符之後的偏移量。
String group();返回由以前匹配操作所匹配的輸入子序列。
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
//運行結果
// 3:jia:6
//7:zhu:10
//32:jia:35
public static void main(String args[]) {
String temp = "da jia zhu yi le,ming tian fang jia le!";
//取出由三個字母組成的單詞
String regex = "\\b[a-zA-Z]{3}\\b";//\\b爲邊界
//將規則編譯成Pattern對象
Pattern p = Pattern.compile(regex);
//和字符串關聯,獲取匹配器對象
Matcher m = p.matcher(temp);
while(m.find()){
System.out.println(m.start()+":"+m.group()+":"+m.end());
}
}
}
練習:
練習一:去結巴
public class Main {
//運行結果:我要學編程
public static void main(String args[]) {
String temp="我我...我我我...我要要要..要要.學學學..學學編.編編..編編程程.程.程程";
temp=temp.replaceAll("\\.","");//去點
temp=temp.replaceAll("(.)\\1+","$1");//去疊詞
System.out.println(temp);
}
}
練習二:ip排序
import java.util.Arrays;
public class Main {
public static void main(String args[]) {
String temp="192.168.1.200 17.10.10.10 3.3.50.3 127.0.0.1";
//排序方法:將所有連續數字加兩個0後去後三位即可排序。
//補兩個0
temp=temp.replaceAll("(\\d+)","00$1");
//保留最後每段3位
temp=temp.replaceAll("0*(\\d{3})","$1");
String[] ips=temp.split(" +");
Arrays.sort(ips);
for (String ip : ips) {
System.out.println(ip.replaceAll("0*(\\d+)","$1"));//去除前面的0
}
}
}
練習三:郵箱驗證
public class Main {
public static void main(String args[]) {
String mail="[email protected]";
//\\w代表字母數字下換線,+代表一個或者多個,@固定字符,@後面一個或者多個字母數字,然後就是.和.後面的2~3個字母,例如.com
//然後就是.com重複1~3遍
String regex="\\w+@[a-zA-Z0-9]+(\\.[a-zA-Z]{2,3}){1,3}";
boolean b=mail.matches(regex);
System.out.println(b);
}
}