AutoIt3正则表达式检测工具:
#NoTrayIcon
Opt("MustDeclareVars", 1)
Local $Test, $Pattern, $Replace, $Flag = 0, $Offset = 1, $Count = 0
Local $Ip1, $Ip2, $Ip3, $Ip4, $Lb1, $Lb2, $Bt1, $Bt2, $Mode="匹配模式"
GUICreate("AutoIt3 正则表达式",300,160)
GUICtrlCreateLabel("测试字符串:",10,13,70,20)
$Ip1=GUICtrlCreateInput($Test,80,10,210,20)
GUICtrlCreateLabel("匹配表达式:",10,43,70,20)
$Ip2=GUICtrlCreateInput($Pattern,80,40,210,20)
$Lb1=GUICtrlCreateLabel(" 输出标志:",10,73,70,20)
$Ip3=GUICtrlCreateInput($Flag,80,70,210,20)
$Lb2=GUICtrlCreateLabel("起始位偏移:",10,103,70,20)
$Ip4=GUICtrlCreateInput($Offset,80,100,210,20)
$Bt1=GUICtrlCreateButton($Mode,50,130,80,25)
$Bt2=GUICtrlCreateButton("输出结果",170,130,80,25)
GUISetState()
While 1
Switch GUIGetMsg()
Case -3
Exit
Case $Bt1
Switch $Mode
Case "匹配模式"
$Mode = "替换模式"
GUICtrlSetData($Lb1, "替换表达式:")
GUICtrlSetData($Ip3, $Replace)
GUICtrlSetData($Lb2, " 替换次数:")
GUICtrlSetData($Ip4, $Count)
Case "替换模式"
$Mode = "匹配模式"
GUICtrlSetData($Lb1, " 输出标志:")
GUICtrlSetData($Ip3, $Flag)
GUICtrlSetData($Lb2, "起始位偏移:")
GUICtrlSetData($Ip4, $Offset)
EndSwitch
GUICtrlSetData($Bt1,$Mode)
Case $Bt2
Local $Return = ""
$Test = GUICtrlRead($Ip1)
$Pattern = GUICtrlRead($Ip2)
If NOT ($Test AND $Pattern) Then ContinueLoop
Switch $Mode
Case "匹配模式"
; GUICtrlRead() 获取的是字符,使用 IsInt() 判断时总是得到结果 0
$Flag = GUICtrlRead($Ip3)
If NOT StringRegExp($Flag, "^[0-4]$") Then
MsgBox(16, "错误", "输出标志只能是 0-4 之间的整数!")
ContinueLoop
EndIf
$Offset = Number(GUICtrlRead($Ip4))
If StringRegExp($Offset, "^[1-9]\d*$") Then
MsgBox(16, "错误", "起始位偏移只能是 1 以上的整数!")
ContinueLoop
EndIf
Dim $_Return = StringRegExp($Test,$Pattern,$Flag,$Offset)
If IsArray($_Return) Then
For $i In $_Return
$Return &= $i & @LF
Next
Else
$Return = $_Return
EndIf
Switch $Flag
Case 0
Switch @error
Case 0
If $Return Then
MsgBox(0, "结果", "存在可匹配字符!")
Else
MsgBox(0, "结果", "无匹配!")
EndIf
Case 2
MsgBox(16, "错误", "匹配表达式第 " & @extended & " 位无效")
Case Else
MsgBox(16, "错误", "@error=" & @error &@LF& "@Extended=" & @extended)
EndSwitch
Case 1 To 4
Switch @error
Case 0
MsgBox(0, "结果", "匹配结果为:" &@LF& $Return &@LF& "@Extended=" & @extended)
Case 1
MsgBox(16, "错误", "无匹配!")
Case 2
MsgBox(16, "错误", "匹配表达式第 " & @extended & " 位无效")
Case Else
MsgBox(16, "错误", "@error=" & @error &@LF& "@Extended=" & @extended)
EndSwitch
EndSwitch
Case "替换模式"
$Replace = GUICtrlRead($Ip3)
$Count = Number(GUICtrlRead($Ip4))
If NOT StringRegExp($Count, "^[1-9]\d*$") Then
MsgBox(16, "错误", "替换次数只能是 1 以上的整数!")
ContinueLoop
EndIf
Dim $Return = StringRegExpReplace($Test,$Pattern,$Replace,$Count)
Switch @error
Case 2
MsgBox(16, "错误", "匹配表达式第 " & @extended & " 位无效")
Case 0
MsgBox(0, "结果", "替换结果为:" & $Return &@LF& "共替换了 " & @extended & " 次")
Case Else
MsgBox(16, "错误", "@error=" & @error &@LF& "@Extended=" & @extended)
EndSwitch
EndSwitch
EndSwitch
WEnd
修正了一些错误,可惜不能上传附件,我重写了一个较完整的,支持输出完整表达式的程序,代码长了点。不过这个也凑活。
[ 本帖最后由 pw4321 于 2008-7-3 00:00 编辑 ] |