Jump to content

StringRegExp and reading from a file


huldu
 Share

Recommended Posts

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!!!"

Link to comment
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!"

Link to comment
Share on other sites

"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!!!"

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!!!"

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!"

Link to comment
Share on other sites

  • 8 years later...

Hi all,

I'm no t struggling with a simillar case.

When I run StringRegExpGUI.exe from the AutoIt program folder, enter text to be checked and pattern, all is good. Even when I let the program load the text to be checked from a file. All works well, match is found and shown.

However if I run the StringRegExp function in my script, I just get invalid array and I'm not able to get it working. Have tried load content of text and phrase from a file, entering it manually, setting up parameters for regex to ArrayMatch and ArrayGlobalMatch ... nothing worked. Can anyone try to look into the code and tell me what am I doing wrong?

Purpose of my script would be to parse HTML file to get required data.

example text:

<div class="row">
      <div class="field float" style="width: 330px;">
        <div class="label">Device type</div>
        <div class="text">EB 8460p</div>
      </div>

example pattern:

(?:class="label">Device type<\/div>\n*.*<div class="text">)(.*)(?:<\/div>)

 

Result of StringRegExpGUI.exe program of the same text and pattern is attached as png image.
The pattern has been tested on the same text (even the whole html file) with StringRegExpGUI.exe and on the https://regexr.com/ web site with success, no errors.

The Code of my script of course:

#include <MsgBoxConstants.au3>
#include <StringConstants.au3>

Local $aArray = 0, $iOffset = 1

$s_text = ""
$s_phrase = ""

;READ TEXT TO BE REGEXP-ed
$f_text = FileOpen ($CmdLine[1])
MsgBox(0,"file read status",$f_text)
$s_text = FileRead($f_text)
MsgBox(0,"file content",$s_text)
FileClose($f_text)

;READ PHRASE FOR THE REGEXP
$f_phrase = FileOpen ($CmdLine[2])
MsgBox(0,"file read status",$f_phrase)
$s_phrase = FileReadLine($f_phrase)
MsgBox(0,"file content",$s_phrase)
FileClose($f_phrase)

While 1
    $aArray = StringRegExp($s_text, $s_phrase, $STR_REGEXPARRAYMATCH)
    IF NOT IsArray($aArray) Then MsgBox(0,"err",@error & " - " & $aArray)
    If @error Then ExitLoop
    For $i = 0 To UBound($aArray) - 1
        MsgBox($MB_SYSTEMMODAL, "RegExp Test with Option 1 - " & $i, $aArray[$i])
    Next
WEnd

Thank you very much for any guidance how to solve it.

Roso

 

StringRegExpGUI result.png

Edited by 1Rosomak
code added
Link to comment
Share on other sites

Works for me :

#include <Constants.au3>

Local $sText = '<div class="row">' & @CRLF & _
      '<div class="field float" style="width: 330px;">' & @CRLF & _
      '  <div class="label">Device type</div>' & @CRLF & _
      '  <div class="text">EB 8460p</div>' & @CRLF & _
      '</div>'

MsgBox ($MB_SYSTEMMODAL,"",StringRegExp ($sText, 'Device type<\/div>\v+.*<div class="text">(.*)<\/div>', $STR_REGEXPARRAYMATCH)[0])

 

Link to comment
Share on other sites

  • 1 month later...
On 7/3/2019 at 4:13 PM, Nine said:

Works for me :

#include <Constants.au3>

Local $sText = '<div class="row">' & @CRLF & _
      '<div class="field float" style="width: 330px;">' & @CRLF & _
      '  <div class="label">Device type</div>' & @CRLF & _
      '  <div class="text">EB 8460p</div>' & @CRLF & _
      '</div>'

MsgBox ($MB_SYSTEMMODAL,"",StringRegExp ($sText, 'Device type<\/div>\v+.*<div class="text">(.*)<\/div>', $STR_REGEXPARRAYMATCH)[0])

 

Thank you, Nine, for your answer, yes, now after changing \n* for \v+ it works.

Link to comment
Share on other sites

@1Rosomak
With the use of "single-line" or "dot all" modifier:

#include <StringConstants.au3>

Global $strText = '<div class="row">' & @CRLF & _
                  '<div class="field float" style="width: 330px;">' & @CRLF & _
                  '<div class="label">Device type</div>' & @CRLF & _
                  '<div class="text">EB 8460p</div>' & @CRLF & _
                  '</div>'

MsgBox($MB_ICONINFORMATION, "", _
       StringRegExp($strText, '(?s)<div class=\h*"label">Device type<\/div>.*?<div class\h*=\h*"text">([^<]*)<\/div>', $STR_REGEXPARRAYMATCH)[0])

:)

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

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