touch_xu 发表于 2012-9-28 06:42:42

求教一个比较复杂的正则

本帖最后由 touch_xu 于 2012-9-28 06:49 编辑

原始数据是这样的:
626C696E6B80100130027F2C0E43330840290976280B1580100230027F5E0E7A170D7C47137F250A5180100330027F5A147F190E7B4E197F1D0B6180100430027F6A13601B0F3230107F220B6A80100530027F67107F1B157F5C197F1A0B0480100630027F5F107F21157F401B7F25127B80100730027977177F1A0F7F421A7F1A0F7B80100830027F580D7F200F593B107F300E3D80100930027F7516771A167F54157F24121980100A30027F6F0D781E0E532D0E7F2A0E3080100B30027F54136714137F56267F1B0E6480100C30027F5D116916107F451C7F1F0C5482C5B130B43030B230B230B130300339B2

原本没有空格和换行,为了清楚点说明,我进行下了整理:
626C696E6B
80100 1 3002 7F2C0E43330840290976280B 15
80100 2 3002 7F5E0E7A170D7C47137F250A 51
80100 3 3002 7F5A147F190E7B4E197F1D0B 61
80100 4 3002 7F6A13601B0F3230107F220B 6A
80100 5 3002 7F67107F1B157F5C197F1A0B 04
80100 6 3002 7F5F107F21157F401B7F2512 7B
80100 7 3002 7977177F1A0F7F421A7F1A0F 7B
80100 8 3002 7F580D7F200F593B107F300E 3D
80100 9 3002 7F7516771A167F54157F2412 19
80100 A 3002 7F6F0D781E0E532D0E7F2A0E 30
80100 B 3002 7F54136714137F56267F1B0E 64
80100 C 3002 7F5D116916107F451C7F1F0C 54
82C5 B130B43030B230B230B130300339B2

我会连续接收到如上面类似的数据!

80100与3002是固定的,后面的1.2.3到C为编号也就是12行,接下来的24字符为有用数据,后面15,51,61等为校验位。

加粗的部分为我想匹配的结果,也就是说我要的结果一定要在626C696E6B之后(包含626C696E6B自身)连续出现的12组数据,当然所在数据都要出现才行,同时这此数据也都必须在82C5之前,怎么都搞不定啊,肯请指导,谢谢!

shenrenba 发表于 2012-9-28 09:43:24

Local $text = "626C696E6B80100130027F2C0E43330840290976280B1580100230027F5E0E7A170D7C47137F250A5180100330027F5A147F190E7B4E197F1D0B6180100430027F6A13601B0F3230107F" & _
                "220B6A80100530027F67107F1B157F5C197F1A0B0480100630027F5F107F21157F401B7F25127B80100730027977177F1A0F7F421A7F1A0F7B80100830027F580D7F200F593B107F300E" & _
                "3D80100930027F7516771A167F54157F24121980100A30027F6F0D781E0E532D0E7F2A0E3080100B30027F54136714137F56267F1B0E6480100C30027F5D116916107F451C7F1F0C5482C" & _
                "5B130B43030B230B230B130300339B2"
$text = StringRegExpReplace(StringRegExpReplace(StringRegExpReplace(StringRegExpReplace($text, "(?i)82C5.+", ""), "(?=80100)", @CRLF), "(?<=80100)|(?=3002)|(?<=3002)", " "), _
                "(?<=3002)(.+?)(?=.{2}\r\n)", "\1 ")
MsgBox(0, 0, $text)

gzh888666 发表于 2012-9-28 11:43:59

#include <Array.au3>
Local $Str = '626C696E6B80100130027F2C0E43330840290976280B1580100230027F5E0E7A170D7C47137F250A5180100330027F5A147F190E7B4E197F1D0B6180100430027F6A13601B0F3230107F220B6A80100530027F67107F1B157F5C197F1A0B0480100630027F5F107F21157F401B7F25127B80100730027977177F1A0F7F421A7F1A0F7B80100830027F580D7F200F593B107F300E3D80100930027F7516771A167F54157F24121980100A30027F6F0D781E0E532D0E7F2A0E3080100B30027F54136714137F56267F1B0E6480100C30027F5D116916107F451C7F1F0C5482C5B130B43030B230B230B130300339B2'
MsgBox(0, '原字符串', $Str)
Local $Test = StringRegExp($str, '(80100.{1}3002.{24}.{2})', 3)
If Not @Error Then MsgBox(0, '匹配数量: ' & UBound($Test), '其中元素为: ' & $Test)
_ArrayDisplay($Test, UBound($Test))
只是凑个热闹,基本没看懂!

gzh888666 发表于 2012-9-28 11:45:00

本帖最后由 gzh888666 于 2012-9-28 12:01 编辑

{:face (254):} 重复帖子改不了?账号还无法退出!

gzh888666 发表于 2012-9-28 11:58:26

本帖最后由 gzh888666 于 2012-9-28 11:59 编辑

账号有问题,要不就是发不了,要不就是好几个帖子!

user3000 发表于 2012-9-28 12:49:12

回复 1# touch_xu
Local $a=ClipGet()
$b = StringRegExp($a, '(626C696E6B(80100\w3002\w{26}){12})82C5', 3)
If Not @error Then MsgBox(0, '', $b)

qinylj 发表于 2012-10-7 16:02:28

如果不分段的话直接(不如6楼严谨)StringRegExp($a, '(626C696E6B.*)82C5', 3)如果需要分段的话就StringRegExp($a, '626C696E6B|80100\w3002\w{24}\w{2}', 3)

lixiaolong 发表于 2012-10-15 09:36:43

本帖最后由 lixiaolong 于 2012-10-15 09:43 编辑

好久没玩au3了,练习练习.


$Str = _
'626C696E6B80100130027F2C0E43330840290976280B1580100230027F5E0E7A170D7C47137F250A5180100330027F5A147F190E7B4E197F1D0B6180100430027F6A13601B0F3230107F220B6A80100530027F67107F1B157F5C197F1A0B0480100630027F5F107F21157F401B7F25127B80100730027977177F1A0F7F421A7F1A0F7B80100830027F580D7F200F593B107F300E3D80100930027F7516771A167F54157F24121980100A30027F6F0D781E0E532D0E7F2A0E3080100B30027F54136714137F56267F1B0E6480100C30027F5D116916107F451C7F1F0C5482C5B130B43030B230B230B130300339B2'


Local $Test = StringRegExpReplace($Str, '(?i)(.*?)(80100)(.)(3002)(\w{24})(\w{2})|(\w{4})(\w{30})', '\1' & @CRLF & '\2 \3 \4 \5 \6 \7 \8')

MsgBox(0,0,$Test)



上面的代码82C5前面会出现空格,加个 $Test = StringRegExpReplace($Test, '(?m)(?<=^)\s+', '') 就可以删除空格.
页: [1]
查看完整版本: 求教一个比较复杂的正则