Jump to content
Sign in to follow this  
Komarto

RegEx Array gone crazy?

Recommended Posts

Komarto

Hi,

I am new to autoit and i tried testing the regex match

opt("TrayIconDebug", 1)

    $Array = StringRegExp ( "<test>Tal1</test><test>Tal2</test><test>Tal3</test><test>Tal4</test>", "<(?i)test>(.*)</test><test>(.*)</test><test>(.*)</test><test>(.*)</test(i?)>" ,  1, 1)

    msgbox(0, "Test", UBound($Array)-1)


    for $i = 0 to UBound($Array)-1; I suppose to have 4 loops, why am i getting 5?
        msgbox(0, "RegExp Test with Option 1 - " & $i, $array[$i])
    Next

I used a regex match and i am suppose to have only 4 loops (as showed by the msgbox), But the loop runs 5 times (I get an empty msgbox).

Why is this happening?

Thanks

komar

Share this post


Link to post
Share on other sites
Authenticity

You're lucky it's only 5 - (.*) means match any char which is not a new line (default option) at least 0 or more times so it can be nothing also lol. Try reading not only the help file's explanation of StringRegExp behavior but also the short tutorial under 'String Regular expression' and use the bottom application for testing purpose...

Share this post


Link to post
Share on other sites
Komarto

You're lucky it's only 5 - (.*) means match any char which is not a new line (default option) at least 0 or more times so it can be nothing also lol. Try reading not only the help file's explanation of StringRegExp behavior but also the short tutorial under 'String Regular expression' and use the bottom application for testing purpose...

But its suppose to capture any charecter between the test tags, even if the tag is empty, right?

If so i suppose to get 4 matches, and if i am using ubound -1 i need to get 4 loop and no extra loop with an empty msgbox.

can someone explain the extra loop?

Share this post


Link to post
Share on other sites
BrettF

Try this:

Opt("TrayIconDebug", 1)

$Array = StringRegExp("<test>Tal1</test><test>Tal2</test><test>Tal3</test><test>Tal4</test>", "<(?i)test>(.*?)</(?i)test>", 3, 1)

MsgBox(0, "Test", UBound($Array) - 1)


For $i = 0 To UBound($Array) - 1; I suppose to have 4 loops, why am i getting 5?
    MsgBox(0, "RegExp Test with Option 1 - " & $i, $Array[$i])
Next

Cheers,

Brett

Share this post


Link to post
Share on other sites
PsaltyDS

This was what I came up with to do the same:

#include <Array.au3>

$RET = StringRegExp("<test>Tal1</test><test>Tal2</test><test>Tal3</test><test>Tal4</test>", "(?:<test>)(.*?)(?:</test>)", 3)
$iErrSav = @error
$iExtSav = @extended
If IsArray($RET) Then
    _ArrayDisplay($RET, "$RET")
Else
    ConsoleWrite("Error = " & $iErrSav & "  Extended = " & $iExtSav & @LF)
EndIf

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.