正則表達式

A:字符
	x 字符 x。舉例:'a'表示字符a
	\\ 反斜線字符。
	\n 新行(換行)符 ('\u000A') 
	\r 回車符 ('\u000D')
	
B:字符類
	[abc] a、b 或 c(簡單類) 
	[^abc] 任何字符,除了 a、b 或 c(否定) 
	[a-zA-Z] a到 z 或 A到 Z,兩頭的字母包括在內(範圍) 
	[0-9] 0到9的字符都包括
	
C:預定義字符類
	. 任何字符。我的就是.字符本身,怎麼表示呢? \.
	\d 數字:[0-9]
	\w 單詞字符:[a-zA-Z_0-9]
		在正則表達式裏面組成單詞的東西必須有這些東西組成

D:邊界匹配器
	^ 行的開頭 
	$ 行的結尾 
	\b 單詞邊界
		就是不是單詞字符的地方。
		舉例:hello world?haha;xixi
	
E:Greedy 數量詞 
	X? X,一次或一次也沒有
	X* X,零次或多次
	X+ X,一次或多次
	X{n} X,恰好 n 次 
	X{n,} X,至少 n 次 
	X{n,m} X,至少 n 次,但是不超過 m 次 


import java.util.Scanner;
/*
 * 正則表達式:符合一定規則的字符串。
 */
public class RegexDemo {
	public static void main(String[] args) {
		// 創建鍵盤錄入對象
		Scanner sc = new Scanner(System.in);
		System.out.println("請輸入你的QQ號碼:");
		String qq = sc.nextLine();
		System.out.println("checkQQ:" + checkQQ(qq));
	}
	public static boolean checkQQ(String qq) {
		// String regex ="[1-9][0-9]{4,14}";
		// //public boolean matches(String regex)告知此字符串是否匹配給定的正則表達式
		// boolean flag = qq.matches(regex);
		// return flag;

		//return qq.matches("[1-9][0-9]{4,14}");
		
		return qq.matches("[1-9]\\d{4,14}");
	}
}

import java.util.Scanner;
/*
 * 判斷功能
 *		String類的public boolean matches(String regex)
 *
 * 需求:
 * 		判斷手機號碼是否滿足要求?
 * 
 * 分析:
 * 		A:鍵盤錄入手機號碼
 * 		B:定義手機號碼的規則
 * 			13436975980
 * 			13688886868
 * 			13866668888
 * 			13456789012
 * 			13123456789
 * 			18912345678
 * 			18886867878
 * 			18638833883
 * 		C:調用功能,判斷即可
 * 		D:輸出結果
 */
public class RegexDemo {
	public static void main(String[] args) {
		//鍵盤錄入手機號碼
		Scanner sc = new Scanner(System.in);
		System.out.println("請輸入你的手機號碼:");
		String phone = sc.nextLine();
		
		//定義手機號碼的規則
		String regex = "1[38]\\d{9}";
		
		//調用功能,判斷即可
		boolean flag = phone.matches(regex);
		
		//輸出結果
		System.out.println("flag:"+flag);
	}
}

import java.util.Scanner;
/*
 * 校驗郵箱
 * 
 * 分析:
 * 		A:鍵盤錄入郵箱
 * 		B:定義郵箱的規則
 * 			[email protected]
 * 			[email protected]
 * 			[email protected]
 * 			[email protected]
 * 			[email protected]
 * 		C:調用功能,判斷即可
 * 		D:輸出結果
 */
public class RegexTest {
	public static void main(String[] args) {
		//鍵盤錄入郵箱
		Scanner sc = new Scanner(System.in);
		System.out.println("請輸入郵箱:");
		String email = sc.nextLine();
		
		//定義郵箱的規則
		//String regex = "[a-zA-Z_0-9]+@[a-zA-Z_0-9]{2,6}(\\.[a-zA-Z_0-9]{2,3})+";
		String regex = "\\w+@\\w{2,6}(\\.\\w{2,3})+";
		
		//調用功能,判斷即可
		boolean flag = email.matches(regex);
		
		//輸出結果
		System.out.println("flag:"+flag);
	}
}

/*
 * 替換功能
 *  	String類的public String replaceAll(String regex,String replacement)
 *  	使用給定的 replacement 替換此字符串所有匹配給定的正則表達式的子字符串。 
 */
public class RegexDemo {
	public static void main(String[] args) {
		// 定義一個字符串
		String s = "helloqq12345worldkh622112345678java";

		// 我要去除所有的數字,用*給替換掉
	//	String regex = "\\d+";
		// String regex = "\\d";
		//String ss = "*";
		
		// 直接把數字幹掉
		String regex = "\\d+";
		String ss = "";

		String result = s.replaceAll(regex, ss);
		System.out.println(result);
	}
}

/*
 * 獲取功能
 *		Pattern和Matcher類的使用
 *		模式和匹配器的基本使用順序
 */
public class RegexDemo {
	public static void main(String[] args) {
		// 模式和匹配器的典型調用順序
		// 把正則表達式編譯成模式對象
		Pattern p = Pattern.compile("a*b");
		// 通過模式對象得到匹配器對象,這個時候需要的是被匹配的字符串
		Matcher m = p.matcher("aaaaab");
		// 調用匹配器對象的功能
		boolean b = m.matches();
		System.out.println(b);
		
		//這個是判斷功能,但是如果做判斷,這樣做就有點麻煩了,我們直接用字符串的方法做
		String s = "aaaaab";
		String regex = "a*b";
		boolean bb = s.matches(regex);
		System.out.println(bb);
	}
}

import java.util.regex.Matcher;
import java.util.regex.Pattern;
/*
 * 獲取功能:
 * 獲取下面這個字符串中由三個字符組成的單詞
 * da jia ting wo shuo,jin tian yao xia yu,bu shang wan zi xi,gao xing bu?
 */
public class RegexDemo2 {
	public static void main(String[] args) {
		// 定義字符串
		String s = "da jia ting wo shuo,jin tian yao xia yu,bu shang wan zi xi,gao xing bu?";
		// 規則
		String regex = "\\b\\w{3}\\b";

		// 把規則編譯成模式對象
		Pattern p = Pattern.compile(regex);
		// 通過模式對象得到匹配器對象
		Matcher m = p.matcher(s);

		// 調用匹配器對象的功能
		// 通過find方法就是查找有沒有滿足條件的子串
		// public boolean find()
		while (m.find()) {
//		  如何得到值呢?
//		  public String group()
			System.out.println(m.group());
		}

		// 注意:一定要先find(),然後才能group()
		// IllegalStateException: No match found
		// String ss = m.group();
		// System.out.println(ss);
	}
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章