Visual Basic > Visual Basic Forums > Visual Basic Language > find a matching line and replace value
Ask a questionAsk a question
 

Answerfind a matching line and replace value

  • Thursday, November 05, 2009 1:45 AMkayatri Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    i have 2 groups of text in my text file

    Bottom
    11.0 28 Red (2 13.10 04.0) [1 16.71 33.3] 130611 2948 229111 28275
    6.5 28 Blue (2 03.00 03.0) [1 05.36 58.3] 132111 -67725 191611 -51225

    2.0 28 Blue (1 05.36 45.3) [1 05.36 58.3] 211111 51225 210454 34691


    Top

    4.5 28 Red (1 16.71 33.3) [1 12.86 53.8] 229111 -28275 198452 -1309

    1.5 28 Blue (1 05.36 58.3) [1 07.06 59.1] 191611 51225 190456 39339

    I need to match value in () from top group to value in [] from bottom group and place the matching line side by side and replace the value in () from top and [] from bottom to T1,T2,T3 and so onn. some times one value in () from top can match with more than one value in [] from bottom. So the file should look like this

    4.5 28 Red (T1 ) [1 12.86 53.8] 229111 -28275 198452 -1309 | 11.0 28 Red (2 13.10 04.0) [T1 ] 130611 2948 229111 28275

    1.5 28 Blue (T2 ) [1 07.06 59.1] 191611 51225 190456 39339 | 6.5 28 Blue (2 03.00 03.0) [T2 ] 132111 -67725 191611 -51225

    1.5 28 Blue (T2 ) [1 07.06 59.1] 191611 51225 190456 39339 |2.0 28 Blue (1 05.36 45.3) [T2] 211111 51225 210454 34691

    But now the files look like this

    4.5 28 Red (T1 ) [1 12.86 53.8] 229111 -28275 198452 -1309 | 11.0 28 Red (2 13.10 04.0) [T1 ] 130611 2948 229111 28275

    1.5 28 Blue (T2 ) [1 07.06 59.1] 191611 51225 190456 39339 | 6.5 28 Blue (2 03.00 03.0) [T2 ] 132111 -67725 191611 -51225

    1.5 28 Blue (T3 ) [1 07.06 59.1] 191611 51225 190456 39339 |2.0 28 Blue (1 05.36 45.3) [T3] 211111 51225 210454 34691

    Because i do the matching and replacing seperatlly. But how can i do the replacement while finding the matching value so that if more than one matching value is found the replacement value will be same? This is my code so far

    Private Function GetLineMatch(ByRef LineToMatch As String, ByRef LineNumber As Integer, ByRef FileContents() As String) As String
    
    
    
            Dim value As String = System.Text.RegularExpressions.Regex.Match(LineToMatch, "\([\d \.]+\)").Value.Replace("("c, "["c).Replace(")"c, "]"c)
    
            If value = String.Empty Then Return String.Empty
    
            Dim i As Integer = 0
    
            For x As Integer = LineNumber To FileContents.Length - 1
    
    
    
    
    
                If FileContents(x).Contains(value) Then
    
                    LineNumber = x + 1
    
    
    
                    
    
                    Return String.Format("{0}   |{2}    ", LineToMatch, LineNumber.ToString(), FileContents(x), x.ToString())
    
                    
    
                End If
    
    
    
    
    
    
    
            Next
    
    
    
            Return String.Empty
    
    
    
        End Function
    
    
    
        Sub wirecheck()
    
            Dim File1() As String = IO.File.ReadAllLines(TextBox1.Text)
    
            Dim SearchLineNumber As Integer
    
            Dim FirstSearch As Boolean
    
            Dim s As String
    
    
    
            Using sf As New IO.StreamWriter("C:\temp2.txt")
    
    
    
                Using sw As New IO.StreamWriter("C:\wirematches.txt")
    
    
    
                    For x As Integer = 0 To File1.Length - 1
    
                        SearchLineNumber = 0
    
                        FirstSearch = True
    
    
    
    
    
                        s = Me.GetLineMatch(File1(x), x, File1)
    
                        Do
    
                            s = Me.GetLineMatch(File1(x), SearchLineNumber, File1)
    
                            If s <> String.Empty Then
    
                                sw.WriteLine(s)
    
                                FirstSearch = False
    
                            End If
    
                        Loop While (s <> String.Empty)
    
                        If FirstSearch = True Then
    
                            sf.WriteLine(File1(x))
    
                        End If
    
    
    
                    Next
    
                End Using
    
            End Using
    
    
    
            
    
        End Sub
    
    Sub replace1()
    
            Dim lines() As String = IO.File.ReadAllLines("C:\wirematches.txt")
    
            For x As Integer = 0 To lines.GetUpperBound(0)
    
                Dim parts() As String = lines(x).Split(New String() {" | "}, StringSplitOptions.None)
    
                parts(0) = Regex.Replace(parts(0), "\(([0-9]|\s|\.)+\)", "(T" & (x + 1).ToString.PadRight(12) & ")")
    
                parts(1) = Regex.Replace(parts(1), "\[([0-9]|\s|\.)+\]", "[T" & (x + 1).ToString.PadRight(12) & "]")
    
                lines(x) = parts(0) & " | " & parts(1)
    
            Next
    
            IO.File.WriteAllLines("C:\wirematches.txt", lines)
    
        End Sub
    
    Pls any one help me
    Thank you

Answers

  • Thursday, November 05, 2009 3:26 AMAcamar Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    The current problem only appears to be totally different from the previous case because you did not construct your previous solution using advanced programming practices such as defining your objects, creating classes with methods to manipulate the objects, modularising your code with functions and subs and separating your activities into logical groups and components.  If your previous solution had been completed in that form you would see that this problem is nearly identical, and would be solved using much the same proccedures (and many of the same data strctures) that you used in that solution. 

    While you continue to process text files as strings in a simple sequential manner, every problem will appear to be brand new and every problem will seem to need a new solution that is constructed from scratch.  

All Replies

  • Thursday, November 05, 2009 2:57 AMAcamar Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    How is this problem different than the problem you posted here:

    http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/f3eabac7-e31a-404d-8f6b-812e91d62b24

    and why can't you use the same procedure that you used in that case to solve this problem now?
  • Thursday, November 05, 2009 3:11 AMkayatri Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    But i think this one is totally diffrent from that. because i need to read the top group first get the value of () and match that value from bottom group with [] then change the value inside () from top And value in [] from bottom to T1,T2....  so on if value in () from top match with more tahn same value in [] from bottom Then the changed value in () and [] must be same like it should be T1 then if no same value exist then only it should go to T2.

    so now i can detect the top group in the file like this
    Sub findmatch()
            Dim FileContents() As String = IO.File.ReadAllLines(TextBox1.Text)
            Dim x As Integer
            For x = 0 To FileContents.Count - 1
                If FileContents(x).Contains("*+*+* Top *+*+*") Then
                    End
                End If
            Next
            For i As Integer = x To FileContents.Count - 1
    
    
                For j As Integer = 0 To x
    
                Next
            Next
        End Sub
    But i dont know how to match the value in () and [] and also change those values and put the both matching lines side by side in diffrent text file
  • Thursday, November 05, 2009 3:26 AMAcamar Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    The current problem only appears to be totally different from the previous case because you did not construct your previous solution using advanced programming practices such as defining your objects, creating classes with methods to manipulate the objects, modularising your code with functions and subs and separating your activities into logical groups and components.  If your previous solution had been completed in that form you would see that this problem is nearly identical, and would be solved using much the same proccedures (and many of the same data strctures) that you used in that solution. 

    While you continue to process text files as strings in a simple sequential manner, every problem will appear to be brand new and every problem will seem to need a new solution that is constructed from scratch.