1. 程式人生 > >Java基礎(四十九)-常用類庫

Java基礎(四十九)-常用類庫

正則表示式

背景

通過之前一系列的分析可以看見,String是一個非常萬能的型別,因為String不僅僅可以支援有各種字串的處理操作,也支援有向各個資料型別的轉換功能,所以在專案的開發之中,只要是使用者輸入的資訊基本都是String表示。於是在向其他資料轉換的時候,為了保證轉換的正確性,往往需要對其進行一些複雜的驗證處理,那麼這種情況下如果只是單純的依靠String類中的方法是非常麻煩的。

1:什麼是正則表示式

在這裡插入圖片描述


public class JavaAPIDemo {
	public static void main(String[] args) throws Exception {
		String str = "123" ;
		if (isNumber(str)) {
			int num = Integer.parseInt(str) ;
			System.out.println(num * 2);
		}
	}
	public static boolean isNumber(String str) {
		char data [] = str.toCharArray() ;
		for (int x = 0 ; x < data.length ; x ++) {
			if (data[x] > '9' || data[x] < '0') {
				return false ;
			}
		}
		return true ;
	}
}


public class Test{
	public static void main(String[] args) throws Exception {
		String str = "123" ;
		if (isNumber(str)) {
			int num = Integer.parseInt(str) ;
			System.out.println(num * 2);
		}
	}
	public static boolean isNumber(String str) {
		char data [] = str.toCharArray() ;
		for (int x = 0 ; x < data.length ; x ++) {
			if (data[x] > '9' || data[x] < '0') {
				return false ;
			}
		}
		return true ;
	}
}

在這裡插入圖片描述

public class JavaAPIDemo {
	public static void main(String[] args) throws Exception {
		String str = "123" ;
		if (str.matches("\\d+")) {
			int num = Integer.parseInt(str) ;
			System.out.println(num * 2);
		}
	}
}

正則表示式在JDK1.4的時候需要使用到正則表示式需要單獨引入*.jar檔案,但是從JDK1.4之後,正則已經預設被JDK支援,提供有java.util.regex開發包,同時針對String類也進行了一些修改,使其可以有方法直接支援正則處理。

2:需要背的正則標記 在這裡插入圖片描述

在這裡插入圖片描述 在這裡插入圖片描述 在這裡插入圖片描述

3:String類對正則的支援

在進行正則表示式大部分處理的情況下都會基於String類完成,有如下方法:

在這裡插入圖片描述

public class Test {
	public static void main(String[] args) throws Exception {
		String str = "a" ;	// 要判斷的資料
		String regex = "a" ;	// 正則表示式
		System.out.println(str.matches(regex));
	}
}
//true
public class Test {
	public static void main(String[] args) throws Exception {
		String str = "c" ;	// 要判斷的資料
		String regex = "[abc]" ;	// 正則表示式
		System.out.println(str.matches(regex));
	}
}
//true
public class JavaAPIDemo {
	public static void main(String[] args) throws Exception {
		String str = "1" ;	// 要判斷的資料
		String regex = "[a-zA-Z]" ;	// 正則表示式
		System.out.println(str.matches(regex));
	}
}
//false
public class JavaAPIDemo {
	public static void main(String[] args) throws Exception {
		String str = "1" ;	// 要判斷的資料
		String regex = "[0-9]" ;	// 正則表示式
		System.out.println(str.matches(regex));
	}
}

public class Test {
	public static void main(String[] args) throws Exception {
		String str = "#" ;	// 要判斷的資料
		String regex = "." ;	// 正則表示式
		System.out.println(str.matches(regex));
	}
}
//true
public class JavaAPIDemo {
	public static void main(String[] args) throws Exception {
		String str = "a\t" ;	// 要判斷的資料
		String regex = "\\D\\s" ;	// 正則表示式
		System.out.println(str.matches(regex));
	}
}
//true
public class Test {
	public static void main(String[] args) throws Exception {
		String str = "" ;	// 要判斷的資料
		String regex = "\\w*" ;	// 正則表示式
		System.out.println(str.matches(regex));
	}
}
//true
public class Test {
	public static void main(String[] args) throws Exception {
		String str = "ax" ;	// 要判斷的資料
		String regex = "\\w{3,}" ;	// 正則表示式
		System.out.println(str.matches(regex));
	}
}
//false

下面通過一些具體的範例來對正則的使用進行說明。

4:實現字串替換(刪除掉非字母與字母)