Jump to content

Regular Expression Help


CygnusX1
 Share

Recommended Posts

Hey, 

I found a piece of code that does nearly everything I want it to do. However it is using Regular Expression and I'm horrible at them. I have tried and tried and read the Help and did what I could but I can't figure out for the life of me how to change the Regular Expression to do what I want.

I'm trying to get list of all the functions in my current running script including the (). So in my example ConsoleWrite would write:

TestCaseOne()

TestCaseTwo()

..

..

$aArray = StringRegExp(FileRead(@ScriptFullPath), '(?ims)^\s*Func\s*([^(]*)', 3)
For $i = 0 To UBound($aArray) - 1 ; Loop through the array.
    ConsoleWrite($aArray[$i] & @lf) 
Next

I could probably spend the rest of the day reading and trying to learn Regular Expression but I thought I'd ask here first.

Thanks for your help.

 

Cygnus

Link to comment
Share on other sites

without regex

$array = filereadtoarray(@ScriptFullPath)

for $i = 0 to ubound($array) - 1
    $sNoWS = stringstripws($array[$i], 1)
    If stringleft($sNoWS , 4) = "Func" Then
        consolewrite($sNoWS & @CRLF)
    EndIf
Next

Func TestCaseOne()
EndFunc

          Func TestCaseTwo()
EndFunc

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Ok, I got the non-regex working, probably easier to maintain going forward. So thanks for that code snippet.

Funny thing though, calling the function from the if statement won't execute the function. 

When I do 

VarType($nNoWS)

It returns a String.

$array = filereadtoarray(@ScriptFullPath)

for $i = 0 to ubound($array) - 1
    $sNoWS = stringstripws($array[$i], 1)
    
    If stringleft($sNoWS , 4) = "Func" Then
    $sNoWS = StringTrimLeft($sNoWS, 5)
        $sNoWS
    EndIf
Next

Func TestCaseOne()
EndFunc

Func TestCaseTwo()
EndFunc

So the above snippet doesn't work.

Edited by CygnusX1

Cygnus

Link to comment
Share on other sites

This pops a msgbox that says "TestCaseOne"  for every line that has Func (as it is told to, if you want to also execute TestCaseTwo you need a different trigger).  So what is not working exactly?

$array = filereadtoarray(@ScriptFullPath)

for $i = 0 to ubound($array) - 1
    $sNoWS = stringstripws($array[$i], 1)
    If stringleft($sNoWS , 4) = "Func" Then
        TestCaseOne()
    EndIf
Next

Func TestCaseOne()
    msgbox(0, '' , "TestCaseOne")
EndFunc

Func TestCaseTwo()
    msgbox(0, '' , "TestCaseTwo")      
EndFunc

and here it is with another counter as you roll through functions

$array = filereadtoarray(@ScriptFullPath)

$x = 1

for $i = 0 to ubound($array) - 1
    $sNoWS = stringstripws($array[$i], 1)

    If stringleft($sNoWS , 4) = "Func" AND $x = 1 Then
        TestCaseOne()
        $x+=1
    EndIf

    If stringleft($sNoWS , 4) = "Func" and $x = 2 Then
        TestCaseTwo()
    EndIf

Next

Func TestCaseOne()
    msgbox(0, '' , "TestCaseOne")
EndFunc

Func TestCaseTwo()
    msgbox(0, '' , "TestCaseTwo")
        exit
EndFunc
Edited by boththose

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

  • Moderators

Ok, I got the non-regex working, probably easier to maintain going forward. So thanks for that code snippet.

Funny thing though, calling the function from the if statement won't execute the function. 

When I do 

VarType($nNoWS)

It returns a String.

$array = filereadtoarray(@ScriptFullPath)

for $i = 0 to ubound($array) - 1
    $sNoWS = stringstripws($array[$i], 1)
    
    If stringleft($sNoWS , 4) = "Func" Then
    $sNoWS = StringTrimLeft($sNoWS, 5)
        $sNoWS
    EndIf
Next

Func TestCaseOne()
EndFunc

Func TestCaseTwo()
EndFunc

So the above snippet doesn't work.

I'm sorry, I'm a bit confused...

How is ^^ that ^^ easier to maintain exactly than:

#include <Array.au3>
 
Global $gszFRead = FileRead(@ScriptFullPath)
Global $gaFuncs = StringRegExp($gszFRead, "(?i)(?:^|\v)\s*func\s*(\w+)\s*\(", 3)
_ArrayDisplay($gaFuncs)

Mind you, the #include <Array.au3> and _ArrayDisplay are only there to show you the output

Edit:

And are you trying to fire functions from strings?  You'll need to look at the Call(); func in the help file for that.

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

yes, we all see the issue (and its not my code).  please run both of my snippets in post #6. 

Edited by boththose

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Exactly I only noticed the String issue after I try to call the function using the String.

When it was printing to the Console I thought it would work but alas, it doesn't?

How would you make it work?

Edited by CygnusX1

Cygnus

Link to comment
Share on other sites

consolewrite($sNoWS)?

Instead of calling the testcase functions in the loop.

Edited by boththose

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

?

#Include <Array.au3>

$res = StringRegExp(FileRead(@ScriptFullPath), 'Func\s*(\w+\([^)]*\))', 3)
 _ArrayDisplay($res)

Func TestCaseZero()
    msgbox(0, "" , "TestCaseZero")
EndFunc

Func TestCaseOne($param1 = "0")
    msgbox(0, $param1 , "TestCaseOne")
EndFunc

Func TestCaseTwo($param1, $param2)
    msgbox(0, $param2 , "TestCaseTwo")
EndFunc
Edited by mikell
Link to comment
Share on other sites

  • Moderators

Wait wait wait wait!!!!!!

@CygnusX1

What EXACTLY do you want to happen and where?

Edit:

@Mikell, if that's it, then UGH! He specifically asked for a list of functions, not functions and their parameters....

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

  • Moderators

Yours is better anyway, it also gets the volatile funcs, I forgot about those.

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 have a script with TestCases, TestCaseOne(), TestCaseTwo(), etc...

I want to have a function gather all the function names and put them in an array.

Then in a For loop I want to call it of the TestCase functions.

$array = filereadtoarray(@ScriptFullPath) ;script path and name

for $i = 0 to ubound($array) - 1 ;go through the array
    $sNoWS = stringstripws($array[$i], 1) ;strip whitespaces

    If stringleft($sNoWS , 4) = "Func" Then
    $sNoWS = StringTrimLeft($sNoWS, 5) ;return the function name with ()
        $sNoWS ;TestCaseOne(), TestCaseTwo() etc...
    EndIf
Next

Func TestCaseOne()
EndFunc

Func TestCaseTwo()
EndFunc

Is that clear?

Cygnus

Link to comment
Share on other sites

Yes, Call works nice with the regex from the first post...

#Include <Array.au3>

$res = StringRegExp(FileRead(@ScriptFullPath), '(?ims)^\s*Func\s*([^(]*)', 3)
_ArrayDisplay($res)
For $i = 0 to UBound($res)-1
   Call($res[$i])
Next

Func TestCaseOne()
    msgbox(0, "" , "TestCaseOne")
EndFunc

Func TestCaseTwo()
    msgbox(0, "" , "TestCaseTwo")
EndFunc
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...