java正则 group() 用法实例

group是针对()来说的,group(0)就是指的整个串,group(1) 指的是第一个括号里的东西,group(2)指的第二个括号里的东西
测试程序:
import java.util.regex.Matcher;  
import java.util.regex.Pattern;  
  
public class GroupIndexAndStartEndIndexTest {  
  
/** 
* @param args 
*/  
public static void main(String[] args) {  
   // TODO Auto-generated method stub  
   String str = "Hello,World! in Java.";  
   Pattern pattern = Pattern.compile("W(or)(ld!)");  
   Matcher matcher = pattern.matcher(str);  
   while(matcher.find()){  
    System.out.println("Group 0:"+matcher.group(0));//得到第0组——整个匹配  
    System.out.println("Group 1:"+matcher.group(1));//得到第一组匹配——与(or)匹配的  
    System.out.println("Group 2:"+matcher.group(2));//得到第二组匹配——与(ld!)匹配的,组也就是子表达式  
    System.out.println("Start 0:"+matcher.start(0)+" End 0:"+matcher.end(0));//总匹配的索引  
    System.out.println("Start 1:"+matcher.start(1)+" End 1:"+matcher.end(1));//第一组匹配的索引  
    System.out.println("Start 2:"+matcher.start(2)+" End 2:"+matcher.end(2));//第二组匹配的索引  
    System.out.println(str.substring(matcher.start(0),matcher.end(1)));//从总匹配开始索引到第1组匹配的结束索引之间子串——Wor  
   }  
}  
  
}  
执行结果:
Group 0:World!  
Group 1:or  
Group 2:ld!  
Start 0:6 End 0:12  
Start 1:7 End 1:9  
Start 2:9 End 2:12  
Wor 
ps:另外一个日志解析例子  正则   1>. 匹配除换行符以外的任意字符 2> * 重复零次或更多次 3> + 重复1次或更多次 4>? 重复0次或一次
/**
	 * 一次解析
	 * 41287ms  汗。。。
	 */
	public static void parseDefaultFormat1_version1(){
		String message = "200019~#!@200010~#!@200002,test1,测试1,F53177B15545D586F3037AB837B1DD51,7B6331E345BAB86E6C8FF11E9BF84E6D,20160503115009,telecom,idNumberRealName,http://111.235.158.136:18080/restful/rp-label/rp-label-status/_verifyUserIdCardInfo/F53177B15545D586F3037AB837B1DD51/pEzSPyo7IvuwLwGdxFAYphTrf5N4vkf6.json,,String=[mdn=18017931410, idNoHash=23d61a9dc0bbf1ef25c63ee3433ccef7, nameHash=b0bf3f670960616be2750c937dae6e5c, idType=idCard, type=clear],{\"code\":403,\"status\":\"FAIL\",\"message\":\"tokenId is not exists,tokenId = pEzSPyo7IvuwLwGdxFAYphTrf5N4vkf6\",\"time\":\"2016-05-03 11:50:10\",\"trace\":\"\"},{\"code\":403,\"status\":\"FAIL\",\"message\":\"tokenId is not exists,tokenId = pEzSPyo7IvuwLwGdxFAYphTrf5N4vkf6\",\"time\":\"2016-05-03 11:50:10\",\"trace\":\"\"},403tokenId is not exists,tokenId = pEzSPyo7IvuwLwGdxFAYphTrf5N4vkf6,1000014,电信集团数据源出现异常,0~#!@0,失败,357~#!@1";
		String Logpattern = "(.*)~#!@(.*)~#!@(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),String=\\[(.*)\\],\\{(.*)\\},\\{(.*)\\},(.*),(.*),(.*),(.*),(.*)~#!@(.*),(.*),(.*)~#!@(.*)";
		Pattern pattern = Pattern.compile(Logpattern);
		Matcher matcher = pattern.matcher(message);
		long starttime = System.currentTimeMillis();
		if(matcher.find()){
			for (int i = 1; i < matcher.groupCount() + 1; i++) {
				System.out.println(matcher.group(i));
				System.out.println("--------------");
			}
		}
		long endtime = System.currentTimeMillis();
		System.out.println("execute time:"+(endtime-starttime)); //
	}
	/**
	 * 分两段 时间18ms+1ms     
	 */
	public static void parseDefaultFormat2__version1(){
     	String message = "200019~#!@200010~#!@200002,test1,测试1,F53177B15545D586F3037AB837B1DD51,7B6331E345BAB86E6C8FF11E9BF84E6D,20160503115009,telecom,idNumberRealName,http://111.235.158.136:18080/restful/rp-label/rp-label-status/_verifyUserIdCardInfo/F53177B15545D586F3037AB837B1DD51/pEzSPyo7IvuwLwGdxFAYphTrf5N4vkf6.json,,String=[mdn=18017931410, idNoHash=23d61a9dc0bbf1ef25c63ee3433ccef7, nameHash=b0bf3f670960616be2750c937dae6e5c, idType=idCard, type=clear],{\"code\":403,\"status\":\"FAIL\",\"message\":\"tokenId is not exists,tokenId = pEzSPyo7IvuwLwGdxFAYphTrf5N4vkf6\",\"time\":\"2016-05-03 11:50:10\",\"trace\":\"\"},{\"code\":403,\"status\":\"FAIL\",\"message\":\"tokenId is not exists,tokenId = pEzSPyo7IvuwLwGdxFAYphTrf5N4vkf6\",\"time\":\"2016-05-03 11:50:10\",\"trace\":\"\"},403tokenId is not exists,tokenId = pEzSPyo7IvuwLwGdxFAYphTrf5N4vkf6,1000014,电信集团数据源出现异常,0~#!@0,失败,357~#!@1";
		String[] messagearr = message.split(",String=");
		messagearr[1] = "String="+messagearr[1];
		String Logpattern_to_String = "(.*)~#!@(.*)~#!@(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*)";
		Pattern pattern = Pattern.compile(Logpattern_to_String);
		Matcher matcher = pattern.matcher(messagearr[0]);
		long starttime = System.currentTimeMillis();
		if(matcher.find()){
			for (int i = 1; i < matcher.groupCount() + 1; i++) {
				System.out.println(matcher.group(i));
				System.out.println("--------------");
			}
		}
	/*  int index = 0;
	 	if (matcher.find()) {
			for (int i = 1; i < matcher.groupCount() + 1; i++) {
				ret.put(logformat[index], matcher.group(i));
				index++;
			}
		}*/
		long endtime = System.currentTimeMillis();
		System.out.println("execute time:"+(endtime-starttime)); //17ms
		/*************************************************************/
		String Logpattern_String_toEnd = "String=\\[(.*)\\],\\{(.*)\\},\\{(.*)\\},(.*),(.*),(.*),(.*),(.*)~#!@(.*),(.*),(.*)~#!@(.*)";
		pattern = Pattern.compile(Logpattern_String_toEnd);
		matcher = pattern.matcher(messagearr[1]);
		starttime = System.currentTimeMillis();
		if(matcher.find()){
			for (int i = 1; i < matcher.groupCount() + 1; i++) {
				System.out.println(matcher.group(i));
				System.out.println("--------------");
			}
		}
	    endtime = System.currentTimeMillis();
		System.out.println("execute time:"+(endtime-starttime)); //1ms
	}





你可能感兴趣的:(java正则 group() 用法实例)