Jump to content

don't quite understand StringRegExp


Recommended Posts

here is the help file's script using option 1:

;Option 1, using offset
$nOffset = 1
While 1
    $array = StringRegExp('<test>a</test> <test>b</test> <test>c</Test>', '<(?i)test>(.*?)</(?i)test>', 1, $nOffset)
    
    If @error = 0 Then
        $nOffset = @extended
    Else
        ExitLoop
    EndIf
    for $i = 0 to UBound($array) - 1
        msgbox(0, "RegExp Test with Option 1 - " & $i, $array[$i])
    Next
WEnd

it outputs a then b then c. i thought by default it puts these values into the array $array but when i tried to display these values after the while loop as shown below eg msgbox (0,"", array[0]), i got an error!

;Option 1, using offset
$nOffset = 1
While 1
    $array = StringRegExp('<test>a</test> <test>b</test> <test>c</Test>', '<(?i)test>(.*?)</(?i)test>', 1, $nOffset)
    
    If @error = 0 Then
        $nOffset = @extended
    Else
        ExitLoop
    EndIf
    for $i = 0 to UBound($array) - 1
        msgbox(0, "RegExp Test with Option 1 - " & $i, $array[$i])
    Next
WEnd

msgbox(0,"",$array[0])

the error is: Subscript used with non-Array variable.

but didn't it just work a second ago within the While loop?

so my question is, how do i retain the values in $array so i can use them for later?

Edited by dwaynek
Link to comment
Share on other sites

You need to read the return value and the function description carefully. As it's stated in the function description:

Flag = 1 or 2 :
@Error Meaning 
0 Array is valid. Check @Extended for next offset 
1 Array is invalid. No matches. 
2 Bad pattern, array is invalid. @Extended = offset of error in pattern.

So somewhere it must fail and set @error to exit the loop, which in this case invalidates the content of $array. To make it possible to record the matches either use a global array and ReDim it and record the $array content each loop, or specify 3 in the third parameter of the StringRegExp() function to search and return global matches.

Link to comment
Share on other sites

You need to read the return value and the function description carefully. As it's stated in the function description:

Flag = 1 or 2 :
@Error Meaning 
0 Array is valid. Check @Extended for next offset 
1 Array is invalid. No matches. 
2 Bad pattern, array is invalid. @Extended = offset of error in pattern.

So somewhere it must fail and set @error to exit the loop, which in this case invalidates the content of $array. To make it possible to record the matches either use a global array and ReDim it and record the $array content each loop, or specify 3 in the third parameter of the StringRegExp() function to search and return global matches.

thanks! i didn't realize that's what "global matches" meant. i knew what a global array or variable was but the way the help doc used the term, i thought it meant something else. sometimes reading docs isn't as helpful as someone else's enlightened explanation. >_< thanks again!
Link to comment
Share on other sites

I expirienced, that this include list is required when using that "Quick Tester" with Autoit v3.3.0.0

#include <GUIConstants.au3>
#include <windowsconstants.au3>
#include <buttonconstants.au3>
#include <comboconstants.au3>
#include <guiedit.au3>
#include <tabconstants.au3>

BTW: The Online Help of Autoit includes a regexp tester:

Autoit -> Tutorials -> String Regular Expression: At the end of this topic you'll find a button labeled "Open StringRegExpGUI.au3".

Regards, Rudi.

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

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...