ARegExp script in honor to Tatyana Day...
"valery_vi" <[email protected]>
| Newsgroups | gmane.comp.windows.autoit.user |
|---|---|
| Message-ID | <[email protected]> |
Hi,
In honor to Tatyana Day I've written AutoIt Regexp Evaluator:
;===================================================
; ARegExp by Valery Ivanov, 25 January 2007
; In honor to Tatyana Day
;===================================================
#include <GUIConstants.au3>
Global Const $WM_COMMAND = 0x0111
Global Const $EN_UPDATE = 0x400
Global Const $EN_SETFOCUS = 0x0100
Global $RegExp, $Target, $PatternValid, $Result
Global $AutoRefresh, $Offset
Global $Mode = 1
; Set up a basic test GUI
$hWnd = GUICreate("ARegExp © Valery Ivanov, 25 January 2007", 400,
450)
$LabelRegExp = GUICtrlCreateLabel("AutoRegExp:", 10, 10, 180, 21)
$AutoRefresh = GUICtrlCreateCheckbox ("Auto-refresh", 310, 10, 100,
20)
$RegExp = GUICtrlCreateEdit("\b\w{4,5}\b(?i)", 10, 30, 280, 21, BitOR
($ES_WANTRETURN,$ES_AUTOVSCROLL))
$PatternValid = GUICtrlCreateLabel('', 310, 30, 100, 21)
GUICtrlSetColor ($PatternValid, 0xFF0000)
GUICtrlSetState ($AutoRefresh, $GUI_CHECKED)
;--------------------------------------------------
$LabelFlag = GUICtrlCreateLabel("Behaviour of func StringRegExp: ",
10, 65, 180, 21)
$Flag = GUICtrlCreateCombo('1: array of matches', 190, 65, 200, 20)
GUICtrlSetData ($Flag, '2: array of matches - Perl/PHP', '1: array of
matches')
GUICtrlSetData ($Flag, '3: array of global matches - old AutoIt', '1:
array of matches')
GUICtrlSetData ($Flag, '4: array of arrays - Perl/PHP', '1: array of
matches')
;--------------------------------------------------
;$LabelOffset = GUICtrlCreateLabel("The string position to start the
match : ", 10, 85, 180, 21)
;$Offset = GUICtrlCreateInput(1, 190, 85, 100, 21)
;--------------------------------------------------
$LabelResult = GUICtrlCreateLabel("The result: ", 10, 90, 100, 21)
$Result = GUICtrlCreateEdit("", 10, 115, 380, 75)
;--------------------------------------------------
$LabelTarget = GUICtrlCreateLabel("Your sample here:", 10, 200, 380,
21)
$Target = GUICtrlCreateEdit("The quick brown fox jumps over the lazy
dog." & @CRLF & "0123456789.:,;(*!?')" & @CRLF & "/\""$%&+-_#", 10,
230, 380, 200)
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
GUISetState()
; Start the main loop
While 1
$Msg = GUIGetMsg()
Select
Case $Msg = $GUI_EVENT_CLOSE
Exit
Case $Msg = $Flag
$Mode = GUICtrlRead($Flag)
Select
case StringInStr($Mode,'Perl/PHP')
$Mode = 2
case StringInStr($Mode,'global')
$Mode = 3
case StringInStr($Mode,'of arrays')
$Mode = 4
case Else
$Mode = 1
endselect
EndSelect
WEnd
func IsPattern($Pattern)
StringRegExp('', $Pattern, 0)
if @Error then
GUICtrlSetData($PatternValid,'Invalid pattern!')
return 0
endif
GUICtrlSetData($PatternValid,'')
return 1
endfunc
; $MatchGlobal = 1
; if GUICtrlRead($Global) = $GUI_CHECKED then $MatchGlobal = 0
func ShowRegExpResult($Pattern)
$TargetContent = GUICtrlRead($Target)
$nOffset = 1
$Match = ''
Select
Case $Mode = 1
While 1
$Matches = StringRegExp($TargetContent, $Pattern, 1, $nOffset)
if @error = 0 Then
$nOffset = @extended
Else
ExitLoop
EndIf
; MsgBox(0,'','UBound($Matches) = ' & UBound($Matches))
for $i = 0 to UBound($Matches) - 1
$Match &= ' Offset - ' & $nOffset & ', SubMatch
#' & $i & ':' & @CrLf & $Matches[$i] & @CrLf
; MsgBox(0,'','$Matches[$i] = ' & $Matches[$i])
next
WEnd
Case $Mode = 2
$Matches = StringRegExp($TargetContent, $Pattern, 2)
for $i = 0 to UBound($Matches) - 1
$Match &= $Matches[$i] & @CrLf
next
Case $Mode = 3
$Matches = StringRegExp($TargetContent, $Pattern, 3)
for $i = 0 to UBound($Matches) - 1
$Match &= $Matches[$i] & @CrLf
next
Case $Mode = 4
$FullMatches = StringRegExp ($TargetContent, $Pattern, 4)
for $i = 0 to UBound($FullMatches) - 1
$Match &= 'Match #' & $i & ': '
$Matches = $FullMatches[$i]
for $j = 0 to UBound($Matches) - 1
$Match &= $Matches[$j] & @CrLf
Next
Next
EndSelect
GUICtrlSetData($Result,$Match)
endfunc
;==================================
Func WM_COMMAND($hWnd, $msg, $wParam, $lParam)
Local $Pattern, $Refresh
Local $nNotifyCode = BitShift($wParam, 16)
Local $nID = BitAND($wParam, 0x0000FFFF)
If $nID = $RegExp Then
Select
Case $nNotifyCode = $EN_UPDATE
$Refresh = 0
if GUICtrlRead($AutoRefresh) = $GUI_CHECKED then
$Refresh = 1
$Pattern = GUICtrlRead($RegExp)
If StringRight($Pattern,2) = @CRLF Then
$Pattern = StringTrimRight($Pattern,2)
GUICtrlSetData($RegExp,$Pattern)
$Refresh = 1
if IsPattern($Pattern) then
ShowRegExpResult($Pattern)
endif
EndIf
if $Refresh and IsPattern($Pattern) then
ShowRegExpResult($Pattern)
endif
EndSelect
EndIf
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_COMMAND
Some info:
January, 25 is memorial of Tatiana the great martyr which is a
patroness of all Tatianas. On January, 25 , back in 1755, Russian
impress Elizabeth the Great founded the Moscow University. Since that
this day has become the most favorite and the merriest holiday of
students.
See also, about MSU
http://www.msu.ru/en/
Enjoy,
Valery