java正則表達式

java中Pattern類對正則表達式進行編譯,Matcher 類則對輸入的字符串進行解析和匹配的引擎。
m.find()方法:如果輸入的字符串中有匹配正則表達式的值,則返回true。
m.groupCount():返回匹配該正則表達式的組的數量
group(0):返回匹配的所有字符串
group(1)返回第一組匹配的 字符串值,group(2)返回第二組匹配的 字符串值…以此類推。

示例

			String delete_path="D:/commonimages/other/1234214214";
			 String str_pattern="^D:/commonimages/([a-z]{3,15})/(\\d{1,})$";
			 Pattern pattern=Pattern.compile(str_pattern);
			 Matcher m=pattern.matcher(delete_path);
			 if(m.find()){
				//匹配的所有的值
				System.out.println(m.group(0));
				//匹配的第一組
				System.out.println(m.group(1));
				//匹配的第二組
				System.out.println(m.group(2));
			 }

注:(\d+)與(\d{1,})相等 ,都表示至少匹配一次純數字

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