.netregexvb.netfileparsing

Help with parsing lines of a file and replacing a substring


I need to replace certain characters in each line of a file. The file is not delimited, but each line has a fixed format. For example, I need to replace 5 questionmarks into 'x's. The 5 questionmarks that need to be replaced in each line are found in position 10. So for example:

Input file:

abdfg trr?????456
g?????dhs?????diu
eerrttyycdhjjhddd

The output file should be:

abdfg trrxxxxx456
g?????dhsxxxxxdiu
eerrttyycdhjjhddd

The output file will be saved as a different file to a specific location

What's the best way to do this in VB.NET (I'm a little new to VB, so any code sample would help)?


Solution

  • One possible solution is to parse each line in the file using a StreamReader (via the ReadLine() method). As you read in each line, you can use a StreamWriter to write the original line out (via the WriteLine(String) method), with one adjustment. If the line meets your replacement requirements, you will use the String.Replace(String, String) method to swap out the old string for the replacement string.

    Here is a solution (compiled and tested with your sample data above). You would still want to add some exception handling (at the very least, ensure the file exists first):

    Public Shared Sub ReplacementExample(string originalFile, string newFile)
    
        ' Read contents of "oringalFile"
        ' Replace 5 ? characters with 5 x characters.
        ' Write output to the "newFile"
        ' Note: Only do so if the ? characters begin at position 10
    
    
        Const replaceMe As String = "?????"
        Const replacement As String = "xxxxx"
    
        Dim line As String = Nothing
    
        Using r As New StreamReader(originalFile)
            Using w As New StreamWriter(newFile)
    
                line = r.ReadLine()
    
                While Not line Is Nothing
                    w.WriteLine(line.Substring(0, 9) + _
                                line.Substring(9).Replace(replaceMe, replacement))
    
                    line = r.ReadLine()
                End While
    
            End Using
        End Using
    End Sub