Java 正規表示式的簡單應用.

悠悠隱於市發表於2011-03-12
package pack.java.thread.atm;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Expression {
	//* + ? {n} {n,m}的意思:
	//*代表0次到多次;
	//+代表1次到多次;
	//?代表0次到1次;
	//{n}代表出現n次;
	//{n,m}代表出現 n 到 m 次;
	
	//\d代表0-9;
	//\D代表非0-9;
	//\s代表空格;
	//\S代表非空格;
	//\w代表A-Z0-9;
	//\W代表非A-Z0-9;
	
	/**
	 * 匹配字串;
	 */
	private void testMethod1(){
		Pattern pattern = Pattern.compile("Java.*",Pattern.CASE_INSENSITIVE);
		Matcher matcher = pattern.matcher("JAVA 程式語言!");
		boolean bool = matcher.matches();
		if(bool){
			System.out.println("匹配成功!");
		}else{
			System.out.println("匹配失敗!");
		}
	}
	
	/***
	 * 分割字串;
	 */
	private void testMethod2(){
		String regx = "[,|]";
		String input = "Hello Java Word Java,Hello ,,My,Name,Is|Java|Sun,.";
		Pattern pattern = Pattern.compile(regx);
		String[] strs = pattern.split(input);
		for(int i = 0;i<strs.length;i++){
			//如果不是空白的時候,則輸出.
			if(!"".equals(strs[i])){
				System.out.println(strs[i]);
			}
		}
	}
	
	/**
	 * 替換;
	 */
	private void testMethod3(){
		String regex = "正規表示式";
		Pattern pattern = Pattern.compile(regex);
		String input = "正規表示式 Hello Word! 正規表示式 Hello Word!";
		Matcher matcher = pattern.matcher(input);
		//替換第一個出現的位置;
		String result = matcher.replaceFirst("Java");
		
		//替換所有;
		//String result = matcher.replaceAll("Java");
		System.out.println(result);
	}
	
	/**
	 * 根據查詢,然後進行替換;
	 */
	private void testMethod4(){
		Pattern pattern = Pattern.compile("正規表示式");
		Matcher matcher = pattern.matcher("正規表示式 Hello World,正規表示式 Hello World");
		StringBuffer sb = new StringBuffer();
		while (matcher.find()) {
			matcher.appendReplacement(sb, "Java");
		}
		matcher.appendTail(sb);
		System.out.println(sb.toString());
	}
	
	/**
	 * 驗證郵箱;
	 */
	private void testMethod5(){
		String regex = "[\\w\\.\\-]+@[\\w\\-]+[\\.]+[\\w]+";
		String email = "363667565@qq.com";
		//Pattern.CASE_INSENSITIVE 忽略大小寫;
		Pattern pattern = Pattern.compile(regex,Pattern.CASE_INSENSITIVE);
		Matcher matcher = pattern.matcher(email);
		if(matcher.matches()){
			System.out.println("是正確的郵箱!");
		}else{
			System.out.println("不是正確的郵箱!");
		}
	}
	
	/**
	 * 去除html標記;
	 */
	private void testMethod6(){
		Pattern pattern = Pattern.compile("<.+?>", Pattern.DOTALL);
		Matcher matcher = pattern.matcher("<font size='5' color = 'red'>樣式</font>");
		//替換成空白;
		String result = matcher.replaceAll("");
		System.out.println(result);
	}
	
	/**
	 * 查詢html中對應的字元;
	 */
	private void testMethod7(){
		String regex = "href=\"(.+?)\"";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher("<a href=\"index.html\">主頁</a>");
		if(matcher.find()){
			System.out.println(matcher.group(1));
		}
	}
	
	/**
	 * 擷取https或者http url;
	 */
	private void testMethod8(){
		String regex = "(http://|https://){1}[\\w\\.\\-/:]+";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher("http://www.baidu.com -> http://www.csdn.net");
		StringBuffer buffer = new StringBuffer();
		while (matcher.find()) {
			buffer.append(matcher.group());
			buffer.append("\r\n");
		}
		System.out.println(buffer.toString());
	}
	
	/**
	 * 替換{0},{1},{2},{3}中的字元;
	 * 
	 */
	private void testMethod9(){
		String msg = "我喜歡的遊戲有:{1},{2},{3},{4},{5}這幾種.!";
		String[] msgArray = new String[]{"泡泡堂","彈彈堂","Acrlive","卡卡跑訂車","西遊記"};
		StringBuffer sb =new StringBuffer();
		sb.append("我喜歡的遊戲有:");
		for(int i = 1;i<=msgArray.length;i++){
			String regex ="\\{"+i+"\\}";
			Pattern pattern =Pattern.compile(regex);
			Matcher matcher= pattern.matcher(msg);
			if(matcher.find()){
				sb.append(msgArray[i-1]+",");
			}
		}
		sb.append("這幾種.!");
		System.out.println(sb.toString());
		
		
		String str = "Java目前的發展史是由{0}年-{1}年"; 
		String[][] object={new String[]{"\\{0\\}","1995"},new String[]{"\\{1\\}","2007"}}; 
		System.out.println(replace(str,object)); 
		
	}
	
	public static String replace(final String sourceString,Object[] object)
		{ 
		String temp=sourceString; 
		for(int i=0;i<object.length;i++){ 
			String[] result=(String[])object[i];
			Pattern pattern = Pattern.compile(result[0]);
			Matcher matcher = pattern.matcher(temp);
			temp=matcher.replaceAll(result[1]); 
		}
		return temp; 
	};
	
	
	public static void main(String[] args) {
		Expression expression = new Expression();
		expression.testMethod1();
		expression.testMethod2();
		expression.testMethod3();
		expression.testMethod4();
		expression.testMethod5();
		expression.testMethod6();
		expression.testMethod7();
		expression.testMethod8();
		expression.testMethod9();
	}
}

 

相關文章