Java正则表达式匹配多行

在编写程序时,往往会遇到需要正则表达式匹配多行的情况,在网上看了好多方法,猛回头,突然发现多行是可以设置的,代码如下。
package com.my.test;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class MyTest {

	static String tt = "rsion=\"1.0\" encoding=\"UTF8\"?>\n<Service>\n<Service_Header>\n<processes currentprocess=\"1\" nextprocess=\"1\" total=\"1\">\n<process id=\"1\">\n<service_sn>1000001000009000001</service_sn>\n<service_id>00010000000100</service_id>\n<requester_id>0002</requester_id>\n<channel_id>01</channel_id>\n</process>\n</processes>\n<branch_id>802777777</branch_id>\n<service_time>20120323153800</service_time>\n<version_id>02</version_id>\n<service_response/>\n</Service_Header>\n<Service_Body>\n<request>\n<HR_ORG_NAME loop_num=\"1\" p_type=\"G\">\n<MANDT>500</MANDT>\n<PLVAR>01</PLVAR>\n<OTYPE>O</OTYPE>\n<OBJID>00000001</OBJID>\n<ISTAT>1</ISTAT>\n<BEGDA>20100101</BEGDA>\n<ENDDA>99991231</ENDDA>\n<LANGU>1</LANGU>\n<SEQNR>000</SEQNR>\n<AEDTM>20110413</AEDTM>\n<MC_SHORT>xxxx</MC_SHORT>\n<MC_STEXT>xxxx</MC_STEXT>\n<DEL/>\n</HR_ORG_NAME>\n<HR_ORG_NAME loop_num=\"2\" p_type=\"G\">\n<MANDT>500</MANDT>\n<PLVAR>01</PLVAR>\n<OTYPE>O</OTYPE>\n<OBJID>00000002</OBJID>\n<ISTAT>1</ISTAT>\n<BEGDA>20100101</BEGDA>\n<ENDDA>99991231</ENDDA>\n<LANGU>1</LANGU>\n<SEQNR>000</SEQNR>\n<AEDTM>20110413</AEDTM>\n<MC_SHORT>xxxx</MC_SHORT>\n<MC_STEXT>xxxx</MC_STEXT>\n<DEL/>\n</HR_ORG_NAME>\n<HR_ORG_NAME loop_num=\"3\" p_type=\"G\">\n<MANDT>500</MANDT>\n<PLVAR>01</PLVAR>\n<OTYPE>O</OTYPE>\n<OBJID>00000003</OBJID>\n<ISTAT>1</ISTAT>\n<BEGDA>20100101</BEGDA>\n<ENDDA>99991231</ENDDA>\n<LANGU>1</LANGU>\n<SEQNR>000</SEQNR>\n<AEDTM>20110413</AEDTM>\n<MC_SHORT>xxxx</MC_SHORT>\n<MC_STEXT>xxxx</MC_STEXT>\n<DEL/>\n</HR_ORG_NAME>\n</request>\n<ext_attributes>\n<T_KEY_LABEL>0502</T_KEY_LABEL>\n<KEY_LABEL>0001</KEY_LABEL>\n</ext_attributes>\n<response/>\n</Service_Body>\n</Service>";
	/**
	 * @param args
	 */
	public static void main(String[] args) {
//		System.out.println(MyTest.class.getResource(".").getPath().substring(1));
//		List<String> tt1 = new ArrayList<String>();
//		tt1.add(tt);
//		writeFile(tt1);
		Matcher  requstMatcher = Pattern.compile("(.*)<[rR][eE][qQ][uU][eE][sS][tT]>(.*)</[rR][eE][qQ][uU][eE][sS][tT]>(.*)",Pattern.MULTILINE | Pattern.DOTALL).matcher(tt);
		String requestContent = "";
		if(requstMatcher.find())
			requestContent =requstMatcher.group(2);
		requstMatcher = Pattern.compile("<(([A-Z]|[a-z]|_|\\d){1,})\\s*loop_num=\"\\d+\"\\s*p_type\\s*=\\s*\"([A-Z]|[a-z])\">").matcher(requestContent);
		String tableName = "";
		if(requstMatcher.find())
			tableName = requstMatcher.group(1);
		requstMatcher = Pattern.compile("<" + tableName + "\\s{1,}loop_num=\"\\d{1,}\"\\s{1,}p_type=\"[a-z]|[A-Z]\">(.*?)>\\s*</" + tableName + ">",Pattern.MULTILINE | Pattern.DOTALL).matcher(requestContent);
		while(requstMatcher.find())
		{
			String datas = requstMatcher.group(1);
			Matcher requstDataMeta = Pattern.compile("<([^/].*?)>").matcher(datas);
			while(requstDataMeta.find())
			{
				String propertyName = requstDataMeta.group(1).trim();
				String val = "";
				Matcher requstItem = Pattern.compile("<" + propertyName + ">(.*?)</" + propertyName,Pattern.MULTILINE).matcher(datas);
				if(requstItem.find())
					val = requstItem.group(1);
				System.out.println("属性是:" + propertyName + " 值是:" + val);
			}
			System.out.println("____________________________________________________________________________________________________________");
		}
		StringBuffer tt = new StringBuffer();
		tt.append("and").append(" ").append("fal").append("=").append("#dal#");
		System.out.println(tt.delete(0, tt.length()).toString());
	}
}

你可能感兴趣的:(java,code)