Java正则表达式简单应用

 

最近采用redbend的dm库进行DM方面的开发工作。库过程在ddms里面打印出类似于以下的log, DM报文难以区分(蓝色部分),不便于分析。于是用正则式从log中提取DM报文内容。

 

11-22 09:23:41.034: INFO/vDM (vdm)(4438): dm\sess\sess_setup.c.600: Replace command coding result 0x0

11-22 09:23:41.044: INFO/vDM (vdm)(4438): dm\sess\sess_utils.c.857:  metaToPcdata format='int'

11-22 09:23:41.044: INFO/vDM (vdm)(4438): dm\sess\sess_utils.c.864:  metaToPcdata type='org.openmobilealliance.dm.firmwareupdate.userrequest'

11-22 09:23:41.054: INFO/vDM (vdm)(4438): dm\sess\sess_utils.c.871:  metaToPcdata mark='indeterminate'

11-22 09:23:41.054: INFO/vDM (vdm)(4438): rdm\sess\sess_prot.c.86: ---------- Command 3 ALERT(1226)

11-22 09:23:41.054: INFO/vDM (vdm)(4438): dm\sess\sess_setup.c.676: FUMO notification alert command coding result 0x0

11-22 09:23:41.064: INFO/vDM (vdm)(4438): dm\sess\sess_setup.c.1317: SESS_setupSession() <<< send message

11-22 09:23:41.064: INFO/vDM (vdm)(4438): dm\sess\sess_comms.c.549: -------------------- Send message

11-22 09:23:41.074: INFO/vDM (vdm)(4438): dm\sess\sess_comms.c.559: Data to write out...

11-22 09:23:41.074: INFO/vDM (vdm)(4438): 0000  3C 3F 78 6D 6C 20 76 65 72 73 69 6F 6E 3D 22 31  <?xml version="1

11-22 09:23:41.094: INFO/vDM (vdm)(4438):    <?xml version="1

11-22 09:23:41.094: INFO/vDM (vdm)(4438): 0010  2E 30 22 20 65 6E 63 6F 64 69 6E 67 3D 22 55 54  .0" encoding="UT

11-22 09:23:41.094: INFO/vDM (vdm)(4438):    .0" encoding="UT

11-22 09:23:41.094: INFO/vDM (vdm)(4438): 0020  46 2D 38 22 3F 3E 3C 53 79 6E 63 4D 4C 20 78 6D  F-8"?><SyncML xm

11-22 09:23:41.094: INFO/vDM (vdm)(4438):    F-8"?><SyncML xm

11-22 09:23:41.094: INFO/vDM (vdm)(4438): 0030  6C 6E 73 3D 27 53 59 4E 43 4D 4C 3A 53 59 4E 43  lns='SYNCML:SYNC

11-22 09:23:41.104: INFO/vDM (vdm)(4438):    lns='SYNCML:SYNC

11-22 09:23:41.104: INFO/vDM (vdm)(4438): 0040  4D 4C 31 2E 32 27 3E 3C 53 79 6E 63 48 64 72 3E  ML1.2'><SyncHdr>

11-22 09:23:41.104: INFO/vDM (vdm)(4438):    ML1.2'><SyncHdr>

11-22 09:23:41.114: INFO/vDM (vdm)(4438): 0050  3C 56 65 72 44 54 44 3E 3C 21 5B 43 44 41 54 41  <VerDTD><![CDATA

11-22 09:23:41.114: INFO/vDM (vdm)(4438):    <VerDTD><![CDATA

11-22 09:23:41.124: INFO/vDM (vdm)(4438): 0060  5B 31 2E 32 5D 5D 3E 3C 2F 56 65 72 44 54 44 3E  [1.2]]></VerDTD>

11-22 09:23:41.124: INFO/vDM (vdm)(4438):    [1.2]]></VerDTD>

11-22 09:23:41.134: INFO/vDM (vdm)(4438): 0070  3C 56 65 72 50 72 6F 74 6F 3E 3C 21 5B 43 44 41  <VerProto><![CDA

11-22 09:23:41.134: INFO/vDM (vdm)(4438):    <VerProto><![CDA

11-22 09:23:41.134: INFO/vDM (vdm)(4438): 0080  54 41 5B 44 4D 2F 31 2E 32 5D 5D 3E 3C 2F 56 65  TA[DM/1.2]]></Ve

11-22 09:23:41.144: INFO/vDM (vdm)(4438):    TA[DM/1.2]]></Ve

11-22 09:23:41.154: INFO/vDM (vdm)(4438): 0090  72 50 72 6F 74 6F 3E 3C 53 65 73 73 69 6F 6E 49  rProto><SessionI

11-22 09:23:41.154: INFO/vDM (vdm)(4438):    rProto><SessionI

11-22 09:23:41.154: INFO/vDM (vdm)(4438): 00A0  44 3E 3C 21 5B 43 44 41 54 41 5B 35 42 5D 5D 3E  D><![CDATA[5B]]>

11-22 09:23:41.164: INFO/vDM (vdm)(4438):    D><![CDATA[5B]]>

11-22 09:23:41.164: INFO/vDM (vdm)(4438): 00B0  3C 2F 53 65 73 73 69 6F 6E 49 44 3E 3C 4D 73 67  </SessionID><Msg

11-22 09:23:41.164: INFO/vDM (vdm)(4438):    </SessionID><Msg

11-22 09:23:41.174: INFO/vDM (vdm)(4438): 00C0  49 44 3E 3C 21 5B 43 44 41 54 41 5B 31 5D 5D 3E  ID><![CDATA[1]]>

11-22 09:23:41.174: INFO/vDM (vdm)(4438):    ID><![CDATA[1]]>

11-22 09:23:41.194: INFO/vDM (vdm)(4438): 00D0  3C 2F 4D 73 67 49 44 3E 3C 54 61 72 67 65 74 3E  </MsgID><Target>

11-22 09:23:41.194: INFO/vDM (vdm)(4438):    </MsgID><Target>

11-22 09:23:41.194: INFO/vDM (vdm)(4438): 00E0  3C 4C 6F 63 55 52 49 3E 3C 21 5B 43 44 41 54 41  <LocURI><![CDATA

11-22 09:23:41.194: INFO/vDM (vdm)(4438):    <LocURI><![CDATA

11-22 09:23:41.194: INFO/vDM (vdm)(4438): 00F0  5B 68 74 74 70 3A 2F 2F 32 31 30 2E 32 31 2E 32  [http://210.21.2

11-22 09:23:41.204: INFO/vDM (vdm)(4438):    [http://210.21.2

11-22 09:23:41.204: INFO/vDM (vdm)(4438): 0100  33 36 2E 31 38 32 2F 7A 78 6D 64 6D 70 2F 64 6D  36.182/zxmdmp/dm

11-22 09:23:41.214: INFO/vDM (vdm)(4438):    36.182/zxmdmp/dm

11-22 09:23:41.214: INFO/vDM (vdm)(4438): 0110  5D 5D 3E 3C 2F 4C 6F 63 55 52 49 3E 3C 2F 54 61  ]]></LocURI></Ta

11-22 09:23:41.214: INFO/vDM (vdm)(4438):    ]]></LocURI></Ta

11-22 09:23:41.224: INFO/vDM (vdm)(4438): 0120  72 67 65 74 3E 3C 53 6F 75 72 63 65 3E 3C 4C 6F  rget><Source><Lo

11-22 09:23:41.224: INFO/vDM (vdm)(4438):    rget><Source><Lo

11-22 09:23:41.234: INFO/vDM (vdm)(4438): 0130  63 55 52 49 3E 3C 21 5B 43 44 41 54 41 5B 49 4D  cURI><![CDATA[IM

11-22 09:23:41.234: INFO/vDM (vdm)(4438):    cURI><![CDATA[IM

11-22 09:23:41.244: INFO/vDM (vdm)(4438): 0140  45 49 3A 30 30 30 34 33 39 34 38 35 36 34 32 39  EI:0004394856429

11-22 09:23:41.244: INFO/vDM (vdm)(4438):    EI:0004394856429

11-22 09:23:41.244: INFO/vDM (vdm)(4438): 0150  39 39 5D 5D 3E 3C 2F 4C 6F 63 55 52 49 3E 3C 2F  99]]></LocURI></

11-22 09:23:41.254: INFO/vDM (vdm)(4438):    99]]></LocURI></

11-22 09:23:41.254: INFO/vDM (vdm)(4438): 0160  53 6F 75 72 63 65 3E 3C 4D 65 74 61 3E 3C 4D 61  Source><Meta><Ma

11-22 09:23:41.254: INFO/vDM (vdm)(4438):    Source><Meta><Ma

11-22 09:23:41.264: INFO/vDM (vdm)(4438): 0170  78 4D 73 67 53 69 7A 65 20 78 6D 6C 6E 73 3D 27  xMsgSize xmlns='

11-22 09:23:41.264: INFO/vDM (vdm)(4438):    xMsgSize xmlns='

11-22 09:23:41.274: INFO/vDM (vdm)(4438): 0180  73 79 6E 63 6D 6C 3A 6D 65 74 69 6E 66 27 3E 3C  syncml:metinf'><

11-22 09:23:41.274: INFO/vDM (vdm)(4438):    syncml:metinf'><

11-22 09:23:41.274: INFO/vDM (vdm)(4438): 0190  21 5B 43 44 41 54 41 5B 33 30 30 30 5D 5D 3E 3C  ![CDATA[3000]]><

11-22 09:23:41.294: INFO/vDM (vdm)(4438):    ![CDATA[3000]]><

11-22 09:23:41.294: INFO/vDM (vdm)(4438): 01A0  2F 4D 61 78 4D 73 67 53 69 7A 65 3E 3C 4D 61 78  /MaxMsgSize><Max

11-22 09:23:41.294: INFO/vDM (vdm)(4438):    /MaxMsgSize><Max

11-22 09:23:41.294: INFO/vDM (vdm)(4438): 01B0  4F 62 6A 53 69 7A 65 20 78 6D 6C 6E 73 3D 27 73  ObjSize xmlns='s

11-22 09:23:41.294: INFO/vDM (vdm)(4438):    ObjSize xmlns='s

11-22 09:23:41.294: INFO/vDM (vdm)(4438): 01C0  79 6E 63 6D 6C 3A 6D 65 74 69 6E 66 27 3E 3C 21  yncml:metinf'><!

11-22 09:23:41.304: INFO/vDM (vdm)(4438):    yncml:metinf'><!

11-22 09:23:41.304: INFO/vDM (vdm)(4438): 01D0  5B 43 44 41 54 41 5B 31 30 30 30 30 30 5D 5D 3E  [CDATA[100000]]>

11-22 09:23:41.314: INFO/vDM (vdm)(4438):    [CDATA[100000]]>

11-22 09:23:41.314: INFO/vDM (vdm)(4438): 01E0  3C 2F 4D 61 78 4F 62 6A 53 69 7A 65 3E 3C 2F 4D  </MaxObjSize></M

11-22 09:23:41.314: INFO/vDM (vdm)(4438):    </MaxObjSize></M

11-22 09:23:41.324: VERBOSE/ELWidget(2705): goNext

11-22 09:23:41.324: VERBOSE/ELWidget(2705): setPos index=17

11-22 09:23:41.324: INFO/vDM (vdm)(4438): 01F0  65 74 61 3E 3C 2F 53 79 6E 63 48 64 72 3E 3C 53  eta></SyncHdr><S

11-22 09:23:41.334: INFO/vDM (vdm)(4438):    eta></SyncHdr><S

11-22 09:23:41.354: INFO/vDM (vdm)(4438): 0200  79 6E 63 42 6F 64 79 3E 3C 41 6C 65 72 74 3E 3C  yncBody><Alert><

11-22 09:23:41.354: INFO/vDM (vdm)(4438):    yncBody><Alert><

11-22 09:23:41.364: INFO/vDM (vdm)(4438): 0210  43 6D 64 49 44 3E 3C 21 5B 43 44 41 54 41 5B 31  CmdID><![CDATA[1

11-22 09:23:41.364: INFO/vDM (vdm)(4438):    CmdID><![CDATA[1

 

 思路很简单, 出现报文的位置都有一个规律, 先是一串16进制数,然后是这一串16进制数对应的字符串(报文内容)。根据这个规律, 得到以下正则式:

.*[[A-F][0-9]]{4}\\s{2}([[A-F][0-9]]{2}\\s{1}){1,16}(.*)

对比log中的规律, 首先是任意个字符,紧接着出现一个四位的16进制数, 接下来有两个空格, 然后出现两位的16进制数, 空格, 两位的16进制数, 空格, ...(最多16次, 最少1次), 最后是任意字符。 我们希望提取出来的是这最后的任意字符。 代码如下。

 

 

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

 

public class ParseVdmLog {

 

public static final int beginIndex = 46;

public static final int beginIndex2 = 96;

public static final String endString = "/SyncML>.";

/**

* @param args

* @throws FileNotFoundException 

*/

public static void main(String[] args) throws FileNotFoundException {

 

if (false) {

System.out.println("args.length = " + args.length);

for (String string : args) {

System.out.println(string);

}

}

if (1 != args.length) {

System.err.println("usage: java com.xxx.cm.ParseVdmLog srcfile destfile");

System.exit(-1);

}

String srcFile = "";

String destFile = "";

if (false) {

srcFile = "c:\\1.txt";

destFile = srcFile.replace(".txt", ".xml");

} else {

srcFile = args[0];

//destFile = args[1];

if (srcFile.startsWith("\"")) {

srcFile = srcFile.substring(1, srcFile.length() - 1);

}

destFile = srcFile.replace(".txt", ".xml");

}

BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(srcFile)));

BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(destFile)));

 

String line = null;

try {

while (null != (line = br.readLine())) {

if (line.contains("Data read in")) {

processStrings(br, bw, true);

//break;

} else if (line.contains("Data to write out")) {

processStrings(br, bw, false);

}

}

} catch (IOException e) {

e.printStackTrace();

}

try {

br.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

bw.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

 

private static void processStrings(BufferedReader in, BufferedWriter out, boolean readIn) throws IOException {

/* String line = null;

String targetStr = "";

// 读取到的第一行为"0000  3C 3F 78 6D 6C 20 76 65 72 73 69 6F 6E 3D 22 31  <?xml version="1"

// 略过该行

while (null != (line = in.readLine()) && null != (line = in.readLine())) {

targetStr = line.substring(beginIndex).trim();

out.append(targetStr);

if (targetStr.equalsIgnoreCase(endString)) {

break ;

}

}

out.append("\r\n");

out.flush();

System.out.println("finish processReadIn()");*/

addComment(out, readIn);

String line2 = null;

String targetStr2 = "";

String patterns = ".*[[A-F][0-9]]{4}\\s{2}([[A-F][0-9]]{2}\\s{1}){1,16}(.*)";

Pattern reg = Pattern.compile(patterns);

while (null != (line2 = in.readLine())) {

Matcher m = reg.matcher(line2);

if (m.matches()) {

//System.out.println(line2);

//break;

//System.out.println(line2.substring(beginIndex2));

//targetStr2 = line2.substring(beginIndex2);

//.*[[A-F][0-9]]{4}\\s{2}([[A-F][0-9]]{2}\\s{1}){1,16}(.*)

// 提取组2中的子串, 即"最后的任意字符串"

targetStr2 = m.group(2);

//System.out.println(m.group(2));

out.append(targetStr2.trim());

if (line2.contains(endString)) {

// 遇到结尾

addComment(out, readIn);

}

}

}

out.newLine();

out.flush();

}

private static void addComment(BufferedWriter out, boolean readIn) throws IOException {

out.newLine();

if (readIn) {

out.append("<!------------------server response----------------------->");

} else {

out.append("<!------------------client request----------------------->");

}

out.newLine();

 

}

}

 

代码从保存在txt文件中的ddms log中提取目标内容(DM报文), 最后将内容保存在一个同名的xml文件中。 用eclipse格式化该xml文件, 最终得到整齐、容易阅读的DM报文。

 

 

 

<!------------------client request----------------------->

<?xml version="1.0" encoding="UTF-8"?>

<SyncML xmlns='SYNCML:SYNCML1.2'>

<SyncHdr>

<VerDTD><![CDATA[1.2]]></VerDTD>

<VerProto><![CDATA[DM/1.2]]></VerProto>

<SessionID><![CDATA[5B]]></SessionID>

<MsgID><![CDATA[1]]></MsgID>

<Target>

<LocURI><![CDATA[http://192.168.0.1/zxmdmp/dm]]></LocURI>

</Target>

<Source>

<LocURI><![CDATA[IMEI:000439485642999]]></LocURI>

</Source>

<Meta>

<MaxMsgSize xmlns='syncml:metinf'><![CDATA[3000]]></MaxMsgSize>

<MaxObjSize xmlns='syncml:metinf'><![CDATA[100000]]></MaxObjSize>

</Meta>

</SyncHdr>

<SyncBody>

<Alert>

<CmdID><![CDATA[1]]></CmdID>

<Data><![CDATA[1201]]></Data>

</Alert>

<Replace>

<CmdID><![CDATA[2]]></CmdID>

<Item>

<Source>

<LocURI><![CDATA[./DevInfo/Mod]]></LocURI>

</Source>

<Meta>

<Format xmlns='syncml:metinf'><![CDATA[chr]]></Format>

</Meta>

<Data><![CDATA[X500]]></Data>

</Item>

<Item>

<Source>

<LocURI><![CDATA[./DevInfo/Man]]></LocURI>

</Source>

<Meta>

<Format xmlns='syncml:metinf'><![CDATA[chr]]></Format>

</Meta>

<Data><![CDATA[123]]></Data>

</Item>

<Item>

<Source>

<LocURI><![CDATA[./DevInfo/Lang]]></LocURI>

</Source>

<Meta>

<Format xmlns='syncml:metinf'><![CDATA[chr]]></Format>

</Meta>

<Data><![CDATA[EN]]></Data>

</Item>

<Item>

<Source>

<LocURI><![CDATA[./DevInfo/DmV]]></LocURI>

</Source>

<Meta>

<Format xmlns='syncml:metinf'><![CDATA[chr]]></Format>

</Meta>

<Data><![CDATA[1.2]]></Data>

</Item>

<Item>

<Source>

<LocURI><![CDATA[./DevInfo/DevId]]></LocURI>

</Source>

<Meta>

<Format xmlns='syncml:metinf'><![CDATA[chr]]></Format>

</Meta>

<Data><![CDATA[IMEI:000439485642999]]></Data>

</Item>

</Replace>

<Alert>

<CmdID><![CDATA[3]]></CmdID>

<Data><![CDATA[1226]]></Data>

<Item>

<Meta>

<Format xmlns='syncml:metinf'><![CDATA[int]]></Format>

<Type xmlns='syncml:metinf'><![CDATA[org.openmobilealliance.dm.firmwareupdate.userrequest]]></Type>

<Markxmlns = syncml:metinf '><![CDATA[indeterminate]]></Mark>

</Meta>

<Data><![CDATA[0]]></Data>

</Item>

</Alert>

<Final />

</SyncBody>

</SyncML>

 

程序不复杂, 不过解决了开发中遇到的问题。 权当一个Java正则表达式的小练习。

你可能感兴趣的:(java,正则表达式)