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