Sign in to follow this  
Followers 0
equilibrium

alpha numeric user name matches

13 posts in this topic

#1 ·  Posted (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 by equilibrium

Share this post


Link to post
Share on other sites



;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

Share this post


Link to post
Share on other sites

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)

Share this post


Link to post
Share on other sites

Awesome! i will try this as soon as I can and get back to you thank you very much.

Share this post


Link to post
Share on other sites

Dude omg!!! you are an AutoIT god!!! it worked beautifully!!!

Thank You soo much!!!!!

Share this post


Link to post
Share on other sites

#6 ·  Posted (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 by equilibrium

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

#9 ·  Posted (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 by weaponx

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

#11 ·  Posted (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 by weaponx

Share this post


Link to post
Share on other sites

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 :)

Share this post


Link to post
Share on other sites

#13 ·  Posted (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 :lmao: i am starting to feel like an AutoIT geek :party:, 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 by equilibrium

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