本帖最后由 zch11230 于 2013-1-11 09:36 编辑
一个专业工具的发送和返回数据包,公司内部的不太方便说名字,因为配套的软件功能比较局限,研究了一下这些数据,已经得到了需要的内容,文本内容比较多,通常是达10W+行,开始用的笨办法一行一行的读,能达到效果太效率太差了 试了一下正则,一个文件一秒都要不到,速度爽多了,但是在正则匹配更多内容的时候遇到了麻烦,不知该如何写
需要得到的数据就3个,以第40行中的10 07 17 00 09 0C为标志
1:标志行最临近的日期和时间,前后无所谓
2:第56行第34和35个字符 也就是01,规律是该行位于标志行后的第16行 或者是标志行后第二个以00000020h:开头的行,这个是不变的 。
前面两个数据我的正则是10 07 17 00 09 0C.+\n.+\n.+\n.+\n(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}).+\n.+\n.+\n.+\n.+\n.+\n.+\n.+\n.+\n.+\n.+\n.+\n.{33}(.{2})
现在我需要第3个数据,规律是标志行向前面数,第一个以10 07 10 00 03 或者 10 07 10 00 04开头的行,需要得到是03还是04,示例中在第二行的25和26字符,为03,这一行的位置不固定,因为中间有可能会穿插一些其它数据包。
还有一个要求如果有多个符合条件的要都匹配出来
我的(?s)10 07 10 00 (03|04) 0C.+?10 07 17 00 09 0C.+?(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}).+?00000020h:.{23}(.{2})
03|04是匹配的从开头向下数最近的 而不是从标志行处倒着往上数最近的。示例中一开头就是需要的03,所以看似正确,但实际中如果前面还有以04开头的行 就匹配成04了,求正则达人给个准备的表达式或者指点一二,感谢!2012-12-23 19:10:39 SEND
00000000h: 10 07 10 00 03 0C 17 13 - 0A 27 00 00 00 00 00 00
00000010h: 00 00 00 00 F7 5E 00 10 - 08 FD D6
2012-12-23 19:10:39 RECV
00000000h: 10 06 F7 5E 00
2012-12-23 19:10:39 SEND
00000000h: 10 05 F8 5E 00
2012-12-23 19:10:41 RECV
00000000h: 10 07 35 00
2012-12-23 19:10:41 RECV
00000000h: 03 79 50 78 70 78 70 98 - 19 FF 3F 56 50 00 00 00
00000010h: 00 27 1F 00 00 00 90 E7 - 6B 3B 10 00 00 00 00 00
00000020h: 00 00 00 00 00 12 80 08 - 15 01 01 01 00 00 00 00
00000030h: 00 FF A1 00 00 F8 5E 00 - 10 08 83 73
2012-12-23 19:10:41 SEND
00000000h: 10 07 10 00 07 0C 17 13 - 0A 29 00 00 00 00 00 00
00000010h: 00 00 00 00 F9 5E 00 10 - 08 D0 AF
2012-12-23 19:10:41 RECV
00000000h: 10 06 F9 5E 00
2012-12-23 19:10:41 SEND
00000000h: 10 05 FA 5E 00
2012-12-23 19:10:42 RECV
00000000h: 10 07 35 00
2012-12-23 19:10:42 RECV
00000000h: 07 79 50 78 70 78 70 98 - 19 FF 3F 56 50 00 00 00
00000010h: 00 27 1F 00 00 00 90 E4 - 6B 3B 10 00 00 00 00 00
00000020h: 00 00 00 00 00 02 80 08 - 15 01 01 01 00 00 00 00
00000030h: 00 00 00 00 00 FA 5E 00 - 10 08 8F 20
2012-12-23 19:11:54 SEND
00000000h: 10 07 17 00 09 0C 17 13 - 0B 2A 00 0C 00 00 00 00
00000010h: 00 00 00 00 00 00 00 00 - 00 00 00 0D 5F 00 10 08
00000020h: 8B 30
2012-12-23 19:11:55 RECV
00000000h: 10 06 0D 5F 00
2012-12-23 19:11:56 SEND
00000000h: 10 05 0E 5F 00
2012-12-23 19:12:00 RECV
00000000h: 10 07 A5 00
2012-12-23 19:12:00 RECV
00000000h: 09 79 50 38 70 39 70 98 - 19 FE 3F 56 50 00 00 00
00000010h: 00 67 1F 00 00 00 80 B7 - 69 1B 10 00 00 00 00 00
00000020h: 00 00 00 00 00 00 00 01 - 37 01 01 00 00 00 00 00
00000030h: 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00
00000040h: 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00
00000050h: 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00
00000060h: 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00
00000070h: 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00
00000080h: 00 00 00 00 00 00 00 00 - 00 00 00 01 00 00 00 00
00000090h: 00 01 00 00 00 12 80 08 - 01 01 01 01 00 00 00 00
000000A0h: 00 00 00 00 00 0E 5F 00 - 10 08 AC 0F
|