Link to home
Start Free TrialLog in
Avatar of chadmanvb
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,DUNC,Joe Blow,Non-pilot
UK Branch 20000,userid2,12341,OAG,NBUSKIR,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,OAG,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_scripts\Easypass\pilotstatus.txt")   'set stream to read passwords to reset file

        'read passwords.txt
        Dim sr As New System.IO.StreamReader("\\windows_scripts\Easypass\passwords.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.RegularExpressions.Regex.Replace(input, _
                Pattern, "${N1}" & strPilot.Trim & "${N3}", _
                System.Text.RegularExpressions.RegexOptions.Multiline Or _
                System.Text.RegularExpressions.RegexOptions.Singleline)

        End While

        srset.Close()   'close stream on passwordstoreset file

        MsgBox(input)
Avatar of Zberteoc
Zberteoc
Flag of Canada image

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.
Avatar of chadmanvb
chadmanvb

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
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Works great! Thanks, Chad
Not a problem, glad I was able to help. ;=)