superklamer Posted December 12, 2014 Share Posted December 12, 2014 Hello guys, I need your help again .. I need just a little push it the right direction This is what I am trying to do: I have a text file called "log.txt" that has an X amount of lines. The lines in the file look like this: http://mywebsite.com/page/1 http://mywebsite/page/2 I have a regular expression that goes through the file and searches for a line that has the "page/{number}" in it. I want to output this to an array. Some of the lines in the "log.txt" file might not contain the "page/{number}" so I want to skip them and have only the ones returned by my regular expression in the array. This is what I have until now, and for some reason it is not outputting to an array. I have tried the regular expression in this website https://regex101.com/ and I know it is working because it is returning the values that I want and omitting the ones that I don't want, but how the do I put all that into an array? #include <Array.au3> #include <File.au3> #include <MsgBoxConstants.au3> #include <StringConstants.au3> Local $LogFile = ("log.txt") ;Set a variable for the filename FileOpen($LogFile, 0) ;Open the file with read-only Local $aArray_RegEx[] = [0] ;Create empty array NOT SURE ABOUT THIS ONE ???? Local $aArray_RegEx = StringRegExp($LogFile, "(?is).+page/\b\d{0,}\b" ,$STR_REGEXPARRAYFULLMATCH) ;search an array for link containing "page/{number} and output to array For $i = 0 To UBound($aArray_RegEx) - 1 ;loop through the array and give me a message MsgBox($MB_SYSTEMMODAL, "RegExp Test with Option 2 - " & $i, $aArray_RegEx[$i]) Next Your help is much appreciated guys. I know it's something so easy .... Link to comment Share on other sites More sharing options...
Moderators Solution SmOke_N Posted December 13, 2014 Moderators Solution Share Posted December 13, 2014 (edited) (?ism).+?/page/d+ Edit: BTW, you have to "Read" the file with FileRead(), FileOpen does nothing but return a handle So: #include <Array.au3> Local $LogFile = "log.txt" Local $sRead = FileRead($LogFile) Local $aArray_RegEx = StringRegExp($sRead, "(?ism).+?/page/\d+" ,$STR_REGEXPARRAYFULLMATCH) _ArrayDisplay($aArray_RegEx) Edited December 13, 2014 by SmOke_N superklamer 1 Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
superklamer Posted December 15, 2014 Author Share Posted December 15, 2014 SmOke_N thank you very much. One more question: Why the regular expression search only brings me the first line and it doesn't search the rest of the file? Wasn't the (?m) supposed to search the whole file? Link to comment Share on other sites More sharing options...
superklamer Posted December 15, 2014 Author Share Posted December 15, 2014 SmOke_N thank you very much. One more question: Why the regular expression search only brings me the first line and it doesn't search the rest of the file? Wasn't the (?m) supposed to search the whole file? Figured it out ... It's because the "$STR_REGEXPARRAYGLOBALMATCH" should be used instead of "$STR_REGEXPARRAYFULLMATCH" $STR_REGEXPARRAYGLOBALMATCH finds all the matching instances while, $STR_REGEXPARRAYFULLMATCH will find only the 1st match and return the captured group. Link to comment Share on other sites More sharing options...
superklamer Posted December 17, 2014 Author Share Posted December 17, 2014 Sorry I am revving and older thread, but is there a way to use variables in your regular expression search? For example if I have $VAR = 100 Can I do this? StringRegExp("TEST 100", "(?ism)($VAR)") I want to use variable in my search because I might need to search for different string every now and then. I am getting the $VAR variable from another file, but how do I search for it? Thanks guys! Link to comment Share on other sites More sharing options...
Malkey Posted December 18, 2014 Share Posted December 18, 2014 Sorry I am revving and older thread, but is there a way to use variables in your regular expression search? For example if I have $VAR = 100 Can I do this? StringRegExp("TEST 100", "(?ism)($VAR)") I want to use variable in my search because I might need to search for different string every now and then. I am getting the $VAR variable from another file, but how do I search for it? Thanks guys! Use "&" to concatenates/joins two strings. Local $VAR = 100 ; The "\b" word boundaries prevent 10 or 0 being matched when the $VAR is 100. If StringRegExp("TEST 100", "(?ism)\b" & $VAR & "\b") Then MsgBox(0, "Results", $VAR & " is present in the subject test string") Else MsgBox(0, "Results", $VAR & " is not present in the subject test string") EndIf superklamer 1 Link to comment Share on other sites More sharing options...
superklamer Posted December 18, 2014 Author Share Posted December 18, 2014 That is awesome and so simple!! How did I not think of that??! Thank you, Malkey! Link to comment Share on other sites More sharing options...
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