equilibrium Posted March 25, 2008 Share Posted March 25, 2008 (edited) **WARNING - Very long post** L= letter (not case sensative) N= digits (0-9) Hi guys I'm relatively new to AutoIT and i'm really having difficulty getting to read a text file to only match user accounts. The patterns for user ids can be: LLNNNN or LNNNNN or LLNNNL basically its gotta be 6 characters and alphanumeric but only in the above pattern matches here is what i have so far: CODE #include <File.au3> #include <Array.au3> $textfile = FileOpenDialog("Textfile",@WorkingDir,"Text File (*.txt)") Dim $arrTextFile Dim $output Dim $ltr = "m" Dim $nmr = "1" If Not _FileReadToArray($textfile,$arrTextFile) Then MsgBox(4096,"Error", " Error reading text file. error:" & @error) Exit EndIf For $x = 1 to $arrTextFile[0] ;Loop through text file lines and parse for UIDS If StringRegExp($arrTextFile[$x],"(?i:\b((?=[A-Z])(?=[A-Z]*\d)[A-Z0-9]{6,6})\") Then $output = $output & $arrTextFile[$x] & @CRLF EndIf Next $arrTextFile = 0 ;Cleanup ;Remove leading and trailing spaces $output = StringStripWS ($output,7) ClipPut ($output) ;replace crap with semicolons and no extra spaces $output = StringRegExpReplace ($output,"[^[:alnum:]]",";") msgbox(0,"Semicolons placed, spaces removed.",$output) $tempfile = @WorkingDir & "\temp.txt" FileWrite($tempfile,$output) _FileReadToArray($tempfile,$arrTextFile) FileDelete($tempfile) msgbox(0,"The Result.",$output) ;Debug to show you what I have at this point Do $file = FileSaveDialog("Save As",@WorkingDir,"Text File (*.txt)") Until $file > "" If StringLeft(StringRight($file,4),1) <> "." Then $file = $file & "UserID List.txt" FileWrite($file,$output) Exit the files come in every garbled way possible so i gotta be able to pick and choose only the user names a sample text file: Model User=mu741b. Please provide the same for the same users. My team need access to the Domain =COM Project= COMP. mu74441 vd1350 ks7949 dvd8705 wp5034 cb0133 mmu7310 spbp519 st0309 rk993j mb2565 cg2635 mg1212 sr3939 kl1690 jw6932 sz1750 dl2276 sw9005 model rh0112 am1152 ag3970 LEACS nfsrz3 km8 nfsrz3 model LEACS 192.168.0.1 / 255.255.255.224 any help on this matter would be HUGE please i really dont know what i'm doing wrong. please help. Edited March 25, 2008 by equilibrium Link to comment Share on other sites More sharing options...
weaponx Posted March 25, 2008 Share Posted March 25, 2008 ;Define patterns $pattern1 = "(?i)[[:alpha:]]{2}\d{4}" ;LLNNNN $pattern2 = "(?i)[[:alpha:]]\d{5}" ;LNNNNN $pattern3 = "(?i)[[:alpha:]]{2}\d{3}[[:alpha:]]" ;LLNNNL ;Define final pattern (matches any of the above) $finalPattern = $pattern1 & "|" & $pattern2 & "|" & $pattern3 ;Test names $array = StringSplit("cb0133,mmu7310,spbp519,st0309,rk993j", ",") For $X = 1 to $array[0] ConsoleWrite($array[$X] & ": ") ;Perform match If StringRegexp($array[$X],$finalPattern,0) Then ConsoleWrite("OK") Else ConsoleWrite("FAIL") EndIf ConsoleWrite(@CRLF) Next Link to comment Share on other sites More sharing options...
weaponx Posted March 25, 2008 Share Posted March 25, 2008 This will store all matches found in the text file into an array, rather than reading the file to an array and testing line by line. #include <array.au3> $textfile = "test.txt" $file = FileRead($textfile) ;Define patterns $pattern1 = "(?i)[[:alpha:]]{2}\d{4}" ;LLNNNN $pattern2 = "(?i)[[:alpha:]]\d{5}" ;LNNNNN $pattern3 = "(?i)[[:alpha:]]{2}\d{3}[[:alpha:]]" ;LLNNNL ;Define final pattern (matches any of the above) $finalPattern = $pattern1 & "|" & $pattern2 & "|" & $pattern3 $aMatches = StringRegexp($file,$finalPattern,3) _ArrayDisplay($aMatches) Link to comment Share on other sites More sharing options...
equilibrium Posted March 25, 2008 Author Share Posted March 25, 2008 Awesome! i will try this as soon as I can and get back to you thank you very much. Link to comment Share on other sites More sharing options...
equilibrium Posted March 25, 2008 Author Share Posted March 25, 2008 Dude omg!!! you are an AutoIT god!!! it worked beautifully!!! Thank You soo much!!!!! Link to comment Share on other sites More sharing options...
equilibrium Posted March 26, 2008 Author Share Posted March 26, 2008 (edited) I know this gonna sound silly but now i can't seem to pull the data from the array, like to a variable so i can output to a message box or clipboard or output to a file. i have tried this but it comes up as blank in the end, its just from the help files within autoit CODE Dim $aMatches[10][20] ;element 0,0 to 9,19 $rows = UBound($aMatches) $cols = UBound($aMatches, 2) $dims = UBound($aMatches, 0) MsgBox(0, "The " & $dims & "-the array has", _ $rows & " rows, " & $cols & " columns") ;Display $aMatches' contents $output = "" For $r = 0 to UBound($aMatches,1) - 1 $output = $output & @LF For $c = 0 to UBound($aMatches,2) - 1 $output = $output & $aMatches[$r][$c] & " " Next Next MsgBox(4096,"Array Contents", $output) Edited March 26, 2008 by equilibrium Link to comment Share on other sites More sharing options...
weaponx Posted March 26, 2008 Share Posted March 26, 2008 Is this the entire code or are you still using the example I posted for you? If you are using the example I posted, it returns a one-dimensional array. You can convert it to 2 dimensions using ReDim. Using Dim will flush the contents. This snippet of code you posted doesn't output anything because there is nothing in the array. Link to comment Share on other sites More sharing options...
equilibrium Posted March 26, 2008 Author Share Posted March 26, 2008 Is this the entire code or are you still using the example I posted for you?If you are using the example I posted, it returns a one-dimensional array. You can convert it to 2 dimensions using ReDim. Using Dim will flush the contents.This snippet of code you posted doesn't output anything because there is nothing in the array.You're right, i will try ReDim, and yeah i am still using the example you provided. Link to comment Share on other sites More sharing options...
weaponx Posted March 26, 2008 Share Posted March 26, 2008 (edited) Just as a sidenote, in the code you posted you are doing duplicate work. You already retrieved all of the dimensions, no sense performing another Ubound. $rows = UBound($aMatches, 1) $cols = UBound($aMatches, 2) $dims = UBound($aMatches, 0) ;Display $aMatches' contents $output = "" For $r = 0 to $rows - 1 For $c = 0 to $cols - 1 $output &= $aMatches[$r][$c] & " " Next $output &= @CRLF Next MsgBox(4096,"Array Contents", $output) Edited March 26, 2008 by weaponx Link to comment Share on other sites More sharing options...
equilibrium Posted March 27, 2008 Author Share Posted March 27, 2008 Just as a sidenote, in the code you posted you are doing duplicate work. You already retrieved all of the dimensions, no sense performing another Ubound. $rows = UBound($aMatches, 1) $cols = UBound($aMatches, 2) $dims = UBound($aMatches, 0) ;Display $aMatches' contents $output = "" For $r = 0 to $rows - 1 For $c = 0 to $cols - 1 $output &= $aMatches[$r][$c] & " " Next $output &= @CRLF Next MsgBox(4096,"Array Contents", $output) wow this is harder then I thought, like I said earlier I am pretty new to this concept, I could not not get to view the contents of $aMatches in a message box, nor could I output it to a text file. right now i'm drawing a blank. Link to comment Share on other sites More sharing options...
weaponx Posted March 27, 2008 Share Posted March 27, 2008 (edited) I just went back and read the help file... The number of dimensions must remain the same, or the old array will be forgotten during the ReDim. Tell me what you are trying to store in the second dimension and why its needed. Lets just make a copy of the array: #include <array.au3> $textfile = "test.txt" $file = FileRead($textfile) ;Define patterns $pattern1 = "(?i)[[:alpha:]]{2}\d{4}" ;LLNNNN $pattern2 = "(?i)[[:alpha:]]\d{5}" ;LNNNNN $pattern3 = "(?i)[[:alpha:]]{2}\d{3}[[:alpha:]]" ;LLNNNL ;Define final pattern (matches any of the above) $finalPattern = $pattern1 & "|" & $pattern2 & "|" & $pattern3 ;Retrieve matches $aMatches = StringRegexp($file,$finalPattern,3) ;Display array _ArrayDisplay($aMatches) ;Retrieve element count $numFound = Ubound($aMatches) ;Create new array for duplication Dim $aNew[$numFound][20] ;Copy original array to new array For $X = 0 to $numFound - 1 $aNew[$X][0] = $aMatches[$X] Next Edited March 27, 2008 by weaponx Link to comment Share on other sites More sharing options...
equilibrium Posted March 27, 2008 Author Share Posted March 27, 2008 oh well i think i got something going here for now here is the code: CODE #include <array.au3> #include <File.au3> $textfile = FileOpenDialog("Textfile",@WorkingDir,"Text File (*.txt)") Dim $arrTextFile #cs Dim $output Dim $ltr = "m" Dim $nmr = "1" #ce If Not _FileReadToArray($textfile,$arrTextFile) Then MsgBox(4096,"Error", " Error reading text file. error:" & @error) Exit EndIf ;$textfile = "test.txt" $file = FileRead($textfile) MsgBox(0, "file contents", $file) ;Define patterns $pattern1 = "(?i)[[:alpha:]]{2}\d{4}" ;LLNNNN $pattern2 = "(?i)[[:alpha:]]\d{5}" ;LNNNNN $pattern3 = "(?i)[[:alpha:]]{2}\d{3}[[:alpha:]]" ;LLNNNL ;Define final pattern (matches any of the above) $finalPattern = $pattern1 & "|" & $pattern2 & "|" & $pattern3 $aMatches = StringRegexp($file,$finalPattern,3) #cs MsgBox(0, "RegEX Pattern Being Applied", $finalPattern) ;what it's running against, the regex code MsgBox(0, "file content", $file) #ce _ArraySort($aMatches) _ArrayDisplay($aMatches, "Currently what's in the array") ;output of the user ids $sFile = @ScriptDir & "\users.txt" ; Write first array to file by string file name _FileWriteFromArray($sFile, $aMatches, 1) ; Display results Run("notepad.exe " & $sFile) as you probably figured it out it outputs the array to a predefined text file and opens notepad for me to view it in. i am gonna try what you've just posted as well. will let you know how things turn out, though i may not reply today because work is swamped today Link to comment Share on other sites More sharing options...
equilibrium Posted March 27, 2008 Author Share Posted March 27, 2008 (edited) oh well i think i got something going here for now here is the code: CODE #include <array.au3> #include <File.au3> $textfile = FileOpenDialog("Textfile",@WorkingDir,"Text File (*.txt)") Dim $arrTextFile #cs Dim $output Dim $ltr = "m" Dim $nmr = "1" #ce If Not _FileReadToArray($textfile,$arrTextFile) Then MsgBox(4096,"Error", " Error reading text file. error:" & @error) Exit EndIf ;$textfile = "test.txt" $file = FileRead($textfile) MsgBox(0, "file contents", $file) ;Define patterns $pattern1 = "(?i)[[:alpha:]]{2}\d{4}" ;LLNNNN $pattern2 = "(?i)[[:alpha:]]\d{5}" ;LNNNNN $pattern3 = "(?i)[[:alpha:]]{2}\d{3}[[:alpha:]]" ;LLNNNL ;Define final pattern (matches any of the above) $finalPattern = $pattern1 & "|" & $pattern2 & "|" & $pattern3 $aMatches = StringRegexp($file,$finalPattern,3) #cs MsgBox(0, "RegEX Pattern Being Applied", $finalPattern) ;what it's running against, the regex code MsgBox(0, "file content", $file) #ce _ArraySort($aMatches) _ArrayDisplay($aMatches, "Currently what's in the array") ;output of the user ids $sFile = @ScriptDir & "\users.txt" ; Write first array to file by string file name _FileWriteFromArray($sFile, $aMatches, 1) ; Display results Run("notepad.exe " & $sFile) as you probably figured it out it outputs the array to a predefined text file and opens notepad for me to view it in. i am gonna try what you've just posted as well. will let you know how things turn out, though i may not reply today because work is swamped today i am starting to feel like an AutoIT geek , i've been browsing these forums and really gotta say wow! you got a lot of posts that i'v learned alot from thnx brother man!! Edited March 27, 2008 by equilibrium 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