chadmanvb
asked on
Help with vb regular expressions
I have 2 files csv files that I read. I get the info from the first file and use that to update the second. The first just contains a userid and ldap status and looks like:
userid1,Pilot
userid2,Non-Pilot
The second contains 7 columns and looks like
UK Branch 10000,userid1,12340,FA,DUN C,Joe Blow,Non-pilot
UK Branch 20000,userid2,12341,OAG,NB USKIR,Jan Blow,Pilot
I need to read the first cvs and then update the second csv to look like
UK Branch 10000,userid1,password1,FA ,DUNC,Joe Blow,Pilot
UK Branch 20000,userid2,password2,OA G,NBUSKIR, Jan Blow,Non-Pilot
Below is the code I am using, but it is changing the password instead of the last column. What am I doing wrong?
'read pilotstatus
Dim srset As New StreamReader("\\windows_sc ripts\Easy pass\pilot status.txt ") 'set stream to read passwords to reset file
'read passwords.txt
Dim sr As New System.IO.StreamReader("\\ windows_sc ripts\Easy pass\passw ords.txt") 'set stream to read passwords file
Dim input As String = sr.ReadToEnd()
sr.Close()
While Not srset.EndOfStream()
Dim line() As String = srset.ReadLine().Split("," c)
Dim strID As String = line(0)
Dim strPilot As String = line(1)
' Set up a Regex pattern to locate the data to be changed in the file
Dim Pattern As String = "(?<N1>^.*?,\s*" & strID.Trim & "\s*,)(?<N2>.*?)(?<N3>,.*$ )"
' Locate and replace the data
input = System.Text.RegularExpress ions.Regex .Replace(i nput, _
Pattern, "${N1}" & strPilot.Trim & "${N3}", _
System.Text.RegularExpress ions.Regex Options.Mu ltiline Or _
System.Text.RegularExpress ions.Regex Options.Si ngleline)
End While
srset.Close() 'close stream on passwordstoreset file
MsgBox(input)
userid1,Pilot
userid2,Non-Pilot
The second contains 7 columns and looks like
UK Branch 10000,userid1,12340,FA,DUN
UK Branch 20000,userid2,12341,OAG,NB
I need to read the first cvs and then update the second csv to look like
UK Branch 10000,userid1,password1,FA
UK Branch 20000,userid2,password2,OA
Below is the code I am using, but it is changing the password instead of the last column. What am I doing wrong?
'read pilotstatus
Dim srset As New StreamReader("\\windows_sc
'read passwords.txt
Dim sr As New System.IO.StreamReader("\\
Dim input As String = sr.ReadToEnd()
sr.Close()
While Not srset.EndOfStream()
Dim line() As String = srset.ReadLine().Split(","
Dim strID As String = line(0)
Dim strPilot As String = line(1)
' Set up a Regex pattern to locate the data to be changed in the file
Dim Pattern As String = "(?<N1>^.*?,\s*" & strID.Trim & "\s*,)(?<N2>.*?)(?<N3>,.*$
' Locate and replace the data
input = System.Text.RegularExpress
Pattern, "${N1}" & strPilot.Trim & "${N3}", _
System.Text.RegularExpress
System.Text.RegularExpress
End While
srset.Close() 'close stream on passwordstoreset file
MsgBox(input)
Why don't you just use the Split() method for the second line as well and than rewrite it after you inserted/modified the element you wanted. It is much easier than using regulare expressions.
ASKER
Well that sounds good, but I am not sure how to do that. I am still a little new to all this. Chad
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Works great! Thanks, Chad
Not a problem, glad I was able to help. ;=)