一、正則表達式的特點:
好處:可以簡化對字符串的複雜操作 弊端:符號定義越多,正則越長,閱讀性越差。
主要用途:
* 1,匹配
* 其實就是使用string類的matches()方法
* 2,切割
* 其實就是使用string類的split()方法
* 3,替換
* 其實就是使用string類的replaceAll()方法
* 4,獲取
二、常用操作字符
(1)字符
x 字符 x
\\ 反斜線字符
\0n 帶有八進制值 0 的字符 n (0<= n <= 7)
\0nn 帶有八進制值 0 的字符 nn (0<= n <= 7)
\0mnn 帶有八進制值 0 的字符 mnn(0 <= m<= 3、0 <= n <= 7)
\xhh 帶有十六進制值 0x 的字符 hh
\uhhhh 帶有十六進制值 0x 的字符 hhhh
\t 製表符 ('\u0009')
\n 新行(換行)符 ('\u000A')
\r 回車符 ('\u000D')
\f 換頁符 ('\u000C')
\a 報警 (bell) 符 ('\u0007')
\e 轉義符 ('\u001B')
\cx 對應於 x 的控制符
(2)字符類
[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](減去)
(3)預定義字符類
. 任何字符(與行結束符可能匹配也可能不匹配)
\d 數字:[0-9]
\D 非數字: [^0-9]
\s 空白字符:[ \t\n\x0B\f\r]
\S 非空白字符:[^\s]
\w 單詞字符:[a-zA-Z_0-9]
\W 非單詞字符:[^\w]
(4)邊界匹配器
^ 行的開頭
$ 行的結尾
\b 單詞邊界
\B 非單詞邊界
\A 輸入的開頭
\G 上一個匹配的結尾
\Z 輸入的結尾,僅用於最後的結束符(如果有的話)
\z 輸入的結尾
(5)Greedy數量詞
X? X,一次或一次也沒有
X* X,零次或多次
X+ X,一次或多次
X{n} X,恰好 n 次
X{n,} X,至少 n 次
X{n,m} X,至少 n 次,但是不超過 m 次
三、案例演示
1,匹配
(1)匹配手機號碼是否正確
/**
* 演示匹配
*/
public static void test1(){
//匹配手機號碼是否正確
String tel="13126674217";
String regex="1[358][0-9]{9}";//或者 String regex="1[358]\\d{9}";
boolean matches = tel.matches(regex);
System.out.println(matches);
}
(2)對郵箱進行校驗
/**
* 對郵箱進行校驗
*/
private static void test2() {
String str="[email protected]";
String regex="[a-zA-Z0-9_]+@[a-zA-Z0-9]+(\\.[a-zA-z]{1,3})+";
boolean matches = str.matches(regex);
System.out.println(matches);
}
2,切割
(1)按照空格進行切割
public static void test1(){
String str="張三 小強 週六";
String regex=" +";//出現一次或多次
String[] split = str.split(regex);
for (int i = 0; i < split.length; i++) {
System.out.println(split[i]);
}
}
(2)按照 . 進行切割
public static void test2(){
String str="張三 . 小強 . 週六";
String regex="\\.+";
String[] split = str.split(regex);
for (int i = 0; i < split.length; i++) {
System.out.println(split[i].trim());
}
}
(3)按照有重複字符的進行切割
public static void test3(){
String str="zhangsanmmmmmmmzhaosittttttxiaohong";
String regex="(.)\\1+";//(.):代表組 \\1:代表組的序號
String[] split = str.split(regex);
for (int i = 0; i < split.length; i++) {
System.out.println(split[i].trim());
}
}
3,替換
(1)替換重複字符(最後輸出結果:zhangsan zhaosi xiaohong)
public static void test1(){
String str="zhangsanmmmmmmmmmmmzhaositttttttttttxiaohong";
String regex="(.)\\1+";//第一組 ():代表組 \\1:代表組的序號
//$1 代表:獲取已有正則表達式的第一組作爲參數
String s = str.replaceAll(regex,"$1 ");
System.out.println(s);
}
(2)把手機號碼13126674217 替換成131****4217 這種格式
public static void test2(){
String str1="13126674217";//131****4217
String regex="(\\d{3})\\d{4}(\\d{4})";
//$n 代表:獲取已有正則表達式的第n組作爲參數 n代表從1開始的整數
String s = str1.replaceAll(regex, "$1***$2");
System.out.println(s);
}
(3)治療口吃
private static void test3() {
String str="我我....我我.....要要要.....要學學學編程程程....";
String regex="\\.+";
//替換所有.
String s = str.replaceAll(regex, "");
//第一組的任意字符出現1次或者多次
String regex2="(.)\\1+";
String s1 = s.replaceAll(regex2, "$1");
System.out.println(s1);
}
4、獲取
/**
* 演示獲取
*/
private static void test4() {
String str="ming tian bu yong shang ke le !";
String regx="[a-z]{4} ";
Pattern pattern=Pattern.compile(regx);
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group());
}
}