huldu Posted December 25, 2010 Posted December 25, 2010 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!!!"
GEOSoft Posted December 25, 2010 Posted December 25, 2010 For $i = 0 To UBound($array) -1 MsgBox(0,"", "Value: " & $array[$i]) Next George Reveal hidden contents 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!"
huldu Posted December 25, 2010 Author Posted December 25, 2010 (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" <edit> Maybe it isnt possible to read a value from a variable in the StringRegExp? Seriously i have no idea! Edited December 25, 2010 by huldu "I'm paper, rock is fine, nerf scissors!!!"
MvGulik Posted December 25, 2010 Posted December 25, 2010 (edited) huldu: On 12/25/2010 at 9:59 PM, 'Deathbringer said: 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 December 25, 2010 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 ...
huldu Posted December 25, 2010 Author Posted December 25, 2010 (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 December 25, 2010 by huldu "I'm paper, rock is fine, nerf scissors!!!"
MvGulik Posted December 25, 2010 Posted December 25, 2010 (edited) On 12/25/2010 at 11:13 PM, 'huldu said: 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. Quote $Read being identical to the string i wrote. Edited December 25, 2010 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 ...
GEOSoft Posted December 26, 2010 Posted December 26, 2010 (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 December 26, 2010 by GEOSoft George Reveal hidden contents 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!"
1Rosomak Posted July 3, 2019 Posted July 3, 2019 (edited) 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 Edited July 3, 2019 by 1Rosomak code added
Nine Posted July 3, 2019 Posted July 3, 2019 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]) “They did not know it was impossible, so they did it” ― Mark Twain Reveal hidden contents Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
1Rosomak Posted August 6, 2019 Posted August 6, 2019 On 7/3/2019 at 2: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]) Expand Thank you, Nine, for your answer, yes, now after changing \n* for \v+ it works.
Nine Posted August 6, 2019 Posted August 6, 2019 “They did not know it was impossible, so they did it” ― Mark Twain Reveal hidden contents Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
FrancescoDiMuro Posted August 6, 2019 Posted August 6, 2019 @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: Reveal hidden contents ALWAYS GOOD TO READ: Forum Rules Forum Etiquette
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now