Sign in to follow this  
Followers 0
huldu

StringRegExp and reading from a file

7 posts in this topic

Ive been trying to get StringRegExp to read a value from a log but it just doesnt work at all.

$array = StringRegExp('You have earned 521 experience!', '(?i)You have earned (.*?) (?i)experience!', 3)
MsgBox(0,"", "Value: " & $array[0])

This works great, it extracts the value that can later be used.

However this does not work:

$File = FileOpen(@ScriptDir & "\test.log", 0)
$Read = FileReadLine($File, 1)

$array = StringRegExp($Read, '(?i)You have earned (.*?) (?i)experience!', 3)

MsgBox(0,"", "Value: " & $array)

This only returns a true or false. The lines are identical in the two examples but when i use a variable in StringRegExp it only returns if it was true or false, it doesnt extract the number. What am i doing wrong?


"I'm paper, rock is fine, nerf scissors!!!"

Share this post


Link to post
Share on other sites



For $i = 0 To UBound($array) -1
    MsgBox(0,"", "Value: " & $array[$i])
Next


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

"Subscript used with non-Array variable."

I just do not understand. When i read a line from a file the result is identical to the first example, yet the results are different. When i do a $Read = "You have earned 512 experience!" it works fine. But the second i read the value from the file(and the input is exactly the same) it no longer works. Not sure i actually understand what is going on between the $Read = FileLineRead and doing a $Read = "text" :x

<edit> Maybe it isnt possible to read a value from a variable in the StringRegExp? Seriously i have no idea!

Edited by huldu

"I'm paper, rock is fine, nerf scissors!!!"

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

huldu:

StringRegExp returns an array, and you are not accessing it properly in your second example. Compare the MsgBox line between your two sets of code.

Your code:

MsgBox(0,"", "Value: " & $array[0])
MsgBox(0,"", "Value: " & $array)

Also see Arrays section in http://www.autoitscript.com/autoit3/docs/intro/lang_variables.htm

Edited by MvGulik

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

It shows the same error. But i think the problem may be in the string capture part? If i change:

$array = StringRegExp($Read, '(?i)You have earned (.*?) (?i)experience!', 3)

To

$array = StringRegExp($Read, '(.*?) (?i)experience!', 3)

It runs the script but the result looks like this "You've earned x" then it cuts off. What i am trying to accomplish is to have the "You have earned" removed along with "experience!" but the value in between should be saved. When i do regular read from a string i specify it works. But when get the value from a file it no longer works. For example..

array = StringRegExp('You have earned 521 experience!', '(?i)You have earned (.*?) (?i)experience!', 3)

Works... but

$array = StringRegExp($Read, '(?i)You have earned (.*?) (?i)experience!', 3)

Will not work. $Read being identical to the string i wrote.

<edit> What i am trying to do is create a tool that reads a log in real time. But right now im stuck just getting the values from the log.

Edited by huldu

"I'm paper, rock is fine, nerf scissors!!!"

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

When i do regular read from a string i specify it works. But when get the value from a file it no longer works.

Than logic dictates that the following assumption must be wrong.

$Read being identical to the string i wrote.

Posted Image Edited by MvGulik

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Try this

$hFile = FileOpen(@ScriptDir & "\test.log", 0)
$aLine = StringRegExp(FileReadLine($hFile, 1), "(?i)^.*earned\s*(\d+)\s*experience.*$", 1)
If Not @Error Then
    MsgBox(4096, "Result", $aLine[0])
Else
    MsgBox(4096, "Error", "Error: " & @Error & @CRLF & "Extended: " & @Extended) ;; The help file will tell you what is happening here.
EndIf
FileClose($hFile

There is also a PCRE Toolkit in my signature that might help you.

Edit: forgot to close the file handle.

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

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