[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 271
  • Last Modified:

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)
0
chadmanvb
Asked:
chadmanvb
  • 2
  • 2
1 Solution
 
ZberteocCommented:
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.
0
 
chadmanvbAuthor Commented:
Well that sounds good, but I am not sure how to do that.  I am still a little new to all this.  Chad
0
 
Fernando SotoCommented:
Try it this way

        'read pilotstatus
        Dim srset As New StreamReader("C:\Temp\pilotstatus.txt")   'set stream to read passwords to reset file

        'read passwords.txt
        Dim sr As New System.IO.StreamReader("C:\Temp\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).Trim()
            Dim strPilot As String = line(1).Trim()

            ' Set up a Regex pattern to locate the data to be changed in the file
            Dim Pattern As String = "(^[^,]+,)(?<Pilot>" & strID & ",)([^,]+,[^,]+,[^,]+,[^,]+,)(?<Status>.*?)$"

            ' Locate and replace the data
            input = System.Text.RegularExpressions.Regex.Replace(input, _
                Pattern, "$1${Pilot}$2" & strPilot, _
                System.Text.RegularExpressions.RegexOptions.Multiline)

        End While

        srset.Close()   'close stream on passwordstoreset file

        MsgBox(input)

Fernando
0
 
chadmanvbAuthor Commented:
Works great! Thanks, Chad
0
 
Fernando SotoCommented:
Not a problem, glad I was able to help. ;=)
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now