Sign in to follow this  
Followers 0
Vyrticl

StringRegExp Problem

12 posts in this topic

#1 ·  Posted (edited)

I'm writing some code to search through a file using a pattern and then print out the found string. I first made this code using the string trimming functions but the StringRegExp in the new betas should do this much better. However when I set the flag to 3 it doesn't return an array like it's supposed to. Whenever I try to print an element of the array I have the result stored in it gives me the error, "Subscript used with non-Array variable." Am I doing something wrong or is this a bug in the StringRegExp function?

edit: I forgot to mention I'm using v3.1.1.76 (beta)

$needle = "frames rendered in \d+.\d+ seconds = \d+.\d+ fps"

    $file = FileOpen("qconsole.log", 0)
    $file_contents = FileReadLine($file)
    While @error <> -1
        $file_contents &= FileReadLine($file)
    WEnd
    FileClose($file)

    $result_array = StringRegExp($file_contents, $needle, 3)
    If @Extended Then
        MsgBox(0, "Debug", $result_array[0])
        $result = ""
        For $temp In $result_array
            $result &= " " & $temp
        Next
    Else
        $result = "Couldn't find the needle in the haystack"
    EndIf

    MsgBox(0, "Result", $result)

    Exit
Edited by Vyrticl

Share this post


Link to post
Share on other sites



You have not defined any groups. Do this and see:

$needle = "frames rendered in (\d+.\d+) seconds = (\d+.\d+) fps"

Who else would I be?

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Well This might work better if you show a part of the file wich it is reading (one or two lines)

Because your regexp seems weird.

The error is logical if it cannot find a match in the string it does not make a array but a empty string

So before you try reading the array first check it.

IsArray ( variable )

Parameters

variable The variable/expression to check.

Return Value

Success: Returns 1.

Failure: Returns 0 if parameter is not an array variable.

Also the regexp

"frames rendered in \d+.\d+ seconds = \d+.\d+ fps"

would match somthing like

"frames rendered in 333T53244 seconds = 423423_47672 fps"

You used the . in the expression but . means any char so as you can see I used a letter and a underscore there

I think you mean a different match like:

"frames rendered in 333.53244 seconds = 423423.47672 fps"

Right? just add two extra chars

regexp should be like this (according to regexp definition I remember)

"frames rendered in \d+\.\d+ seconds = \d+\.\d+ fps"

I made this to test

$input  = "frames rendered in 333.53244 seconds = 423423.47672 fps "
$input  &= "frames rendered in 346.2244 seconds = 4423.47672 fps";line 2 for testing

$regexp = "(frames rendered in \d+\.\d+ seconds = \d+\.\d+ fps)"
$output = StringRegExp ( $input, $regexp, 3)

$error = @Error
if ( $error > 0 ) then
  if ($error = 1 ) then msgbox(48,"Problem","Flag invalid. Flag must be one of the numbers in the helpfile. Return value is """".")
  if ($error = 2 ) then msgbox(48,"Problem","Pattern invalid. Return value is the first location in the string that was invalid, as an integer.")
  exit;===why go on, its wrong...
endif

$extended = @Extended
if ( $extended = 0 ) then
  msgbox(48,"Problem","Match not found. Return value is """" (empty string).")
  exit;===no match found so we quit
endif

;for more savemeasure
if ( NOT isarray($output)) then 
  msgbox(48,"Still wrong thats not right","It doens't seem to be an array wich is weird. because" & @CRLF & "@error: " & $error & @CRLF & "@extended: " & $extended & @CRLF & "$output: " & $output)
else
  msgbox(64,"Ow joy - it works", $output[0] & @crlf & $output[1])
endif

I fixed mine also now with () and it gets full lines (what use would that be)

just place the () arround the thing you really want

ow yeah

\d+\.\d+ matches only numbers wich always have a . and decimals so not 2413 and 2 but only 0.0 to 323123123.43242897 (ofcourse even further)

check if the result are always with a point and decimal chars

otherwise try

$regexp = "(frames rendered in \d+.* seconds = \d+.* fps)"

wich will match also if no point or decimal numbers are found

Edited by MrSpacely

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Match found. Return value is an array of all group values. If there are no groups in the pattern, the function returns "" (empty string).

This is exactly what is happening here.

That being said, you are right about the period, and the regexp should be updated to "\." instead of just ".".

Edited by this-is-me

Who else would I be?

Share this post


Link to post
Share on other sites

Yes I forgot the groups because I never used stringregexp in autoit

only stringregexpreplace (used regexp in other languages).

So I forgot. . Just matches all also point indeed. But its wrong;)

evil not good.

Also forgetting about numbers without decimals is not nice for those numbers.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Thanks for all your fast replies. It seems the only problem I had was the missing () around the string I wanted to search for. Once I added those around my search string all worked perfectly.

As for making it so it looks for only decimal numbers is because the number will always have a decimal in it, even it's if 27.0 or whatever.

Edited by Vyrticl

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Thanks for all your fast replies. It seems the only problem I had was the missing () around the string I wanted to search for. Once I added those around my search string all worked perfectly.

Still wrong I wanted to search for . then search for point like \.

. means Match any single character

So it is looking for any character (at that position)

I know it results right but please remember . is like a operator its not "." like a char

I you donot do it right at this moment you are going to get weird trouble with other scripts.

So remember . is not a point but a operator wich equals any one character.

If you donot understand try using .* wich would make any line of chars with 0 to a biljon (even further) charaters wich can equal any character from a to z from 0 to 9 from _ to # from ¤ to ¾

try matching with these numbers 45B4 instead of 45.4 both match with \d+.\d+

read the help file

it states

\ Escape a special character (have it match the actual character) or introduce a special character type (see below)

. is a special character (looks simpel dot but its bigger )

Edited by MrSpacely

Share this post


Link to post
Share on other sites

Still wrong I wanted to search for . then search for point like \.

. means Match any single character

so you are looking for any character (only one ofcourse)

I know it results wrong but please remember . is like a operator its not "." like a char

I you donot do it right at this moment you are going to get weird trouble with other scripts.

So remember . is not a point but a operator wich equals any one character.

If you donot understand try using .* wich would make any line of chars with 0 to a biljon (even further) charaters wich can equal any character from a to z from 0 to 9 from _ to # from ¤ to ¾

try matching with these numbers 45B4 instead of 45.4 both match with \d+.\d+

Yeah I do understand . matches any single character. I changed my search string to

$needle = "(frames rendered in \d+.* seconds = \d+.* fps)"

just in case for some reason there doesn't happen to be a decimal in one of the results.

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

great :P

I was affraid something bad would happen ;)

overdid it a bit on my example script :mad2:

Edited by MrSpacely

Share this post


Link to post
Share on other sites

Yes, it will return an array. But array[0] is not the size of the array.

Can anyone tell me how to count the array size?

The following script will encounter an error and exit.

opt('RunErrorsFatal', 0)
$ahref = StringRegExp($buf, $regex, 3)
If @error = 0 AND @Extended = 1 Then
    If $ahref <> "" Then
        For $i = 0 To 1000
            MsgBox(0, $header & " " & $i, $ahref[$i], 1)
            If @error Then ExitLoop
        Next
        MsgBox(0, $header, $i & "End of list")
    EndIf
Else
    MsgBox(0, $header, "Not found")
EndIf

Share this post


Link to post
Share on other sites

Yes, it will return an array. But array[0] is not the size of the array.

Can anyone tell me how to count the array size?

The following script will encounter an error and exit.

opt('RunErrorsFatal', 0)
$ahref = StringRegExp($buf, $regex, 3)
If @error = 0 AND @Extended = 1 Then
    If $ahref <> "" Then
        For $i = 0 To 1000
            MsgBox(0, $header & " " & $i, $ahref[$i], 1)
            If @error Then ExitLoop
        Next
        MsgBox(0, $header, $i & "End of list")
    EndIf
Else
    MsgBox(0, $header, "Not found")
EndIf
See Ubound

Share this post


Link to post
Share on other sites

See Ubound

Thanks a lot.

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