Jump to content

A StringRegExp... again...


Recommended Posts

Hiho,

those RegExp kill me :), here's some sample code:

Save as test.au3 in scriptdir

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Opt('MustDeclareVars', 1)

Example1()
Example2()

; example 1
Func Example1()
    Local $msg

    GUICreate("My GUI"); will create a dialog box that when displayed is centered
    GUISetState(@SW_SHOW); will display an empty dialog box

 ; Run the GUI until the dialog is closed
    While 1
        $msg = GUIGetMsg()

        If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    WEnd
    GUIDelete()
EndFunc;==>Example1

; example 2
Func Example2()
    Local $gui, $background, $pic, $basti_stay, $msg
    Local $sFile=RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\AutoIt v3\AutoIt", "InstallDir") & "\Examples\GUI\logo4.gif"
    
    $gui = GUICreate("Background", 400, 100)
 ; background picture
    $background = GUICtrlCreatePic(@SystemDir & "\oobe\images\mslogo.jpg", 0, 0, 400, 100)
    GUISetState(@SW_SHOW)

 ; transparent MDI child window
    $pic = GUICreate("", 169, 68, 0, 0, $WS_POPUP, BitOR($WS_EX_LAYERED, $WS_EX_MDICHILD), $gui)
 ; transparent pic
    $basti_stay=GUICtrlCreatePic($sFile, 0, 0, 169, 68)
    GUISetState(@SW_SHOW)

    Do
        $msg = GUIGetMsg()

    Until $msg = $GUI_EVENT_CLOSE
EndFunc;==>Example2

And here's what I already tried:

#include <array.au3>
Global $aVars[1]
$aVars[0] = 0

$file = FileOpen("test.au3", 0)

If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

Global $aVars[1]
$aVars[0] = 0

While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    $aMatchesVar = StringRegExp($line, "\$(?i:[A-Z0-9\._%+-]+)", 3); RegEx for Variables
    If IsArray($aMatchesVar) Then
        ReDim $aVars[$aVars[0] + UBound($aMatchesVar)]
        For $n = 0 To UBound($aMatchesVar) - 1
            $aVars[$aVars[0] + $n] = $aMatchesVar[$n]
        Next
        $aVars[0] = UBound($aVars) - 1
    EndIf
WEnd
_ArrayDisplay($aVars)
FileClose($file)

- My first goal is to extract a list of variables from test.au3, identified by starting '$' and terminating...yeah, that might be the hard part :P. The StringRegExp() above captures some, but I would expect a lot more (hell, at least a single) duplicate in the result.

- My second goal is to extract a list of function names with a similar StringRegExp(), identified by starting 'Func' and terminating '(' , that you should be easier, though I also fail there :party:

I loop by line, so duplicates are no issue. Would be nice, if one of the few with the second sight :idea: can give me a helping hand.

Best Regards

Edited by KaFu
Link to comment
Share on other sites

  • Moderators

Variables:

"\$\w+"

Functions:

"(?i)(?:\A|\n)func\s+(\w+)"

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Thanks for the reply,

Variables:

"\$\w+"

Functions:

"(?i)(?:\A|\n)func\s+(\w+)"

but the first captures the same 11 I captured with the RegEx above already, I would expect to have $msg 6 times in the result, but I it there only once. The second RegEx doesnt return anything for me :).
Link to comment
Share on other sites

  • Moderators

Thanks for the reply,

but the first captures the same 11 I captured with the RegEx above already, I would expect to have $msg 6 times in the result, but I it there only once. The second RegEx doesnt return anything for me :).

Huh?

I got 22 vars returned:

#include <array.au3>
Local $a = StringRegExp(ClipGet(), "(\$\w+)", 3)
_ArrayDisplay($a)

ClipGet() obviously used because I didn't want to save the file.

Edit:

I know these work without testing, because I use them often myself :party: ...

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

I got 22 vars returned:

#include <array.au3>
Local $a = StringRegExp(ClipGet(), "(\$\w+)", 3)
_ArrayDisplay($a)

Edit:

I know these work without testing, because I use them often myself :P ...

I see... me bad, an error in my code :) , thanks a bunch :party:.

$aVars[0] = UBound($aVars) - 1

has to be

$aVars[0] = UBound($aVars)

I'm not sure how he does it, I haven't really looked.

Thanks for the hint. He's doing it by straight string comparison, should also be enough for me :idea:.

Best Regards

Link to comment
Share on other sites

#include <Array.au3>

Global $hFile = FileOpen(@ScriptDir & '\test.au3', 0)
Global $sSrc
Global $aVars, $aFuncs

If $hFile = -1 Then Exit MsgBox(0x10, 'Error', '( \/ )' & @LF & '(oO )' & @LF & '(")(")')
$sSrc = FileRead($hFile)
FileClose($hFile)

$sClean = StringRegExpReplace($sSrc, '"(?:[^"]+|"")*"|''(?:[^'']+|'''')*''|;.*', '')
$aVars = StringRegExp($sClean, '\$\w+', 3)
$aFuncs = StringRegExp($sClean, '(?i)(?x)(\bfunc\s+\w+\s*( \( ( (?>[^()]+) | (?2) )* \) ))', 3)

If IsArray($aVars) Then _ArrayDisplay($aVars)
If IsArray($aFuncs) Then _ArrayDisplay($aFuncs)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...