We help IT Professionals succeed at work.

Help with vb regular expressions

284 Views
Last Modified: 2013-11-07
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)
Comment
Watch Question

CERTIFIED EXPERT

Commented:
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.

Author

Commented:
Well that sounds good, but I am not sure how to do that.  I am still a little new to all this.  Chad
Retired
CERTIFIED EXPERT
Distinguished Expert 2017
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Works great! Thanks, Chad
Fernando SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
Not a problem, glad I was able to help. ;=)

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.