Sign in to follow this  
Followers 0
CygnusX1

Regular Expression Help

23 posts in this topic

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

Share this post


Link to post
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

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

Share this post


Link to post
Share on other sites

"(?i)(?:^|v)s*funcs*(w+)s*("


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.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

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

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

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

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.

Share this post


Link to post
Share on other sites

Copy and paste error in my above code. I hard coded TestCaseOne() I replaced it with the variable now.


Cygnus

Share this post


Link to post
Share on other sites

It is easy for me to maintain because I'm not well versed in RegEX. I understand String manipulation.


Cygnus

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

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

Edited by boththose

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

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

consolewrite($sNoWS)?

Instead of calling the testcase functions in the loop.

Edited by boththose

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

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

?

#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

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

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.

Share this post


Link to post
Share on other sites

Oh, yes

In the first post he said that he wants to get the () so if there are params inside, they should be grabbed too - I thought so  :)

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

As I said earlier:

Call($sNoWs)


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.

Share this post


Link to post
Share on other sites

I'm not asking for the parameters, none of my TestCases take parameters.


Cygnus

Share this post


Link to post
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
Sign in to follow this  
Followers 0