1. 程式人生 > >關於正則表示式的相關理解

關於正則表示式的相關理解

在我們工作中經常會遇到正則表示式的相關問題,這篇文章來詳細來介紹正則表示式的相關用法,希望對你有所啟發;

附上鍊接:

http://tool.oschina.net/uploads/apidocs/jquery/regexp.html

http://www.runoob.com/regexp/regexp-syntax.html

下面舉一個例子,希望對你有所啟發:

package test;

public class TestRegx {
	
	public static void main(String[] args) {
		String str="11111111111";
		boolean flag=isSjhm(str);
		System.out.println(flag);
	}

	public static boolean isSjhm(String str) {
		// TODO Auto-generated method stub
		String pattern="(\\+86|86)?[1][3,4,5,7,8][0-9]{9}$";
		/**
		 * 正則\\表示轉義字元,+表示匹配前面的一次或多次,(\\+86|86)?含義是匹配86開頭的0次或1 
         *  次,後面是1開頭,第二個數字只能是3,4,5,7,8
		 * 第三位-最後一位是0-9範圍的數字有9位,$表示結束位
		 * 
		 */
		return isRegexpCheck(str,pattern);
	}

	public static boolean isRegexpCheck(String str, String pattern) {
		// TODO Auto-generated method stub
		if(!isNotNull(str)){
			return true;
		}
		return str.matches(pattern);
	}

	public static boolean isNotNull(String str) {
		// TODO Auto-generated method stub
		if(str!=null && !"".equals(str))
			return true;
		return false;
	}

}