正則表達式及其相關方法
類 Pattern
1. 正則表達式常用的語法
字符:
x字符x; a 表示字符'a'\\反斜線字符\t製表符 ('\u0009')\n新行(換行)符 ('\u000A')\r回車符 ('\u000D')
字符類:
[abc]a、b 或 c(簡單類)[^abc]任何字符,除了 a、b 或 c(否定)[a-zA-Z]a 到 z 或 A 到 Z,兩頭的字母包括在內(範圍)[a-zA-Z_0-9]裏面包含大寫字母或者小寫字母或者數字字符
預定義字符:
. 任何字符 QQ:[email protected] \. 轉義 (本身就表示一個.)\d 數字:[0-9] [0-9]--->\d(不正確) \\d(代碼中正確的寫法)\w 單詞字符:[a-zA-Z_0-9]
java語言單詞組成的一些規則需要\\w
邊界匹配器:
^行的開頭$行的結束\b 單詞邊界hello world?haha;Xixi
Greedy 數量詞:
X? X,一次或一次也沒有 :x出現1次或者0次X* X,零次或多次 :x出現0次或多次X+ X,一次或多次 :x出現1次或多次X{n} X,恰好 n 次 :x恰好出現nX{n,} X,至少 n 次 :x至少出現n次X{n,m} X,至少 n 次,但是不超過 m 次
//郵箱校驗
import java.util.Scanner;
public class 郵箱校驗 {
public static void main(String[] args) {
Scanner x = new Scanner(System.in);
System.out.println("請輸入郵箱:");
String s = x.nextLine();
String a = "[a-zA-Z_0-9]{5,20}+@[a-zA-Z_0-9]{2,6}+\\.[a-zA-Z]{2,3}";
System.out.println(s.matches(a));
}
}
//手機號校驗;
import java.util.Scanner;
public class 手機號校驗 {
public static void main(String[] args) {
System.out.println("請輸入手機號:");
Scanner x = new Scanner(System.in);
String s = x.nextLine();
String a = "[1][8,2]\\d{9}";
System.out.println(s.matches(a));
}
}
2. 類 Pattern
a. 定義:
public final class PatternextendsObject implements Serializable正則表達式的編譯表示形式。
b. 常用的方法:
i. public static Pattern compile(String regex) 將給定的正則表達式編譯成要給模式對象ii. public Matcher matcher(CharSequence input) 創建匹配給定輸入與此模式的匹配
iii. Matches 類:模式匹配
public boolean matches() 嘗試將整個區域與模式匹配。
如果匹配成功,則可以通過 start、end 和 group 方法獲取更多信息
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Pattern類的模式匹配 {
public static void main(String[] args) {
//模式和匹配器對象的典型的順序
//將正則規則編譯成要給模式對象,(會返會一個模式對象)
Pattern p = Pattern.compile("a*b");//需要導包
Matcher m = p.matcher("aabb");//需要導包
//調用匹配器對象的一寫功能
boolean flag =m.matches();
System.out.println(flag);//false
System.out.println("——————————");
//最終是一個判斷功能,使用之前的方式:
String s = "a*b";
String ss = "aaabbb";
boolean flag2= s.matches(ss);
System.out.println(flag2);//false
}
}
3. 和正則表達式有關的String類中的方法
a.a. public String[] split(String regex) 根據給定正則表達式的匹配拆分此字符串。
b. public String replaceAll(String regex,tring replacement) 使用給定的 replacement 替換此字符串所有匹配給定的正則表達式的子字符串。
public class 拆分字符串 {
public static void main(String[] args) {
//定義一個字符串;
String s = "1997-1-1";
// public String[] split (String regex)
// 根據正則表達式的匹配拆分此字符串
String[] a = s.split("-");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]);
}
}
}
b.
public class 替換此字符串所有匹配給定的正則表達式的子字符串 {
public static void main(String[] args) {
//定義一個字符串
String s = "helloworld199711Yang";
//將數字不顯示出來,而用 $代替;
String a = "\\d+";
String ss = "*";
//public String replaceAll(String regex,String replacement)
String b = s.replaceAll(a, ss);
System.out.println(b);
}
}
//String類切割功能和正則表達式有關係
public class RegexTest3 {
public static void main(String[] args) {
//定義一個字符串
String s = "aa,bb,cc" ;
//使用切割功能,以某種規則切割
String[] strArray = s.split(",") ;
for(int x = 0 ; x < strArray.length ; x ++){
System.out.print(strArray[x]);
}
System.out.println();
String s2 = "aa.bb.cc" ;
//使用切割功能
String[] strArray2 = s2.split("\\.") ;
for(int x = 0 ; x < strArray2.length ; x ++){
System.out.print(strArray2[x]);
}
System.out.println();
String s3 = "aa bb cc" ;
//使用切割功能
String[] strArray3 = s3.split(" +") ;
for(int x = 0 ; x < strArray3.length ; x ++){
System.out.print(strArray3[x]);
}
System.out.println();
//在硬盤的路徑的表現形式:\\代表一個\
String s4 = "E:\\JavaSE\\day13\\Code" ;
//切割功能
String[] strArray4 = s4.split("\\\\") ;
for(int x = 0 ; x < strArray4.length ; x ++){
System.out.print(strArray4[x]);
}
System.out.println();
}
}