search and replace the whole line in a flat file

Last post 05-15-2008 11:36 AM by kagome. 2 replies.

Sort Posts:

  • search and replace the whole line in a flat file

    05-14-2008, 6:52 PM
    • Loading...
    • kagome
    • Joined on 03-01-2007, 12:03 PM
    • Posts 81

    Hi All,

     I would like to do a search and replace on a flat file.  i would like to find a wildcard of a string and completely update the line with a new string.

     

    For example my orginal file is:

    Jim and Dog

    George and Cat

    Karen and Rat

    Sam and Puppy

     

    I would like the output of the new file.

    James

    George and Cat

    Karen and Rat

    Sam and Puppy

     

    Here is my code.

     

           
            Dim path As String = "C:\river1.txt"

     

       
            Dim strText As String = File.ReadAllText(path)
            Dim objFile As System.IO.StreamWriter
           

            strText = Replace(strText, "Jim ", "James ")

            objFile = My.Computer.FileSystem.OpenTextFileWriter("C:\river1.txt", False)


            objFile.Write(strText)
            objFile.Close()

     

    Thank you,

     

    -Kagome

     

     

  • Re: search and replace the whole line in a flat file

    05-14-2008, 7:23 PM
    Answer
    • Loading...
    • nmgomes
    • Joined on 04-09-2007, 11:53 PM
    • Portugal
    • Posts 216

    Hi,

    You can simply do 

    Dim regex As Regex = New Regex("^.*Jim.*$", RegexOptions.Multiline)
    strText = regex.Replace(strText, "James")
    
     
    Nuno Gomes [visit my blog]
    Portugal - Europe's West Coast
    [Don't forget to click "Mark as Answer" on the post(s) that helped you.]
  • Re: search and replace the whole line in a flat file

    05-15-2008, 11:36 AM
    • Loading...
    • kagome
    • Joined on 03-01-2007, 12:03 PM
    • Posts 81

    thank you very much.!

Page 1 of 1 (3 items)