Imports System.Text.RegularExpressions
Module Module1
Public strNewPhoneNumber As String = """2126420001"""
Public intRepCount As Integer = 0
Sub Main()
If IO.File.Exists("c:\reports\replaced.log") Then
IO.File.Delete("c:\reports\replaced.log")
End If
Dim srDataFile As New System.IO.StreamReader("c:\reports\data.txt")
Dim strDataFile As String = srDataFile.ReadToEnd
srDataFile.Close()
Dim srPhoneNumbers As New System.IO.StreamReader("c:\realPhoneNumbers.txt")
Dim strPhoneNumbers As String = srPhoneNumbers.ReadToEnd
srDataFile.Close()
Dim reNumbers As Regex = New Regex("(?<=#PhoneNumber\r\n"")\d+(?="")")
Dim mcNumbers As MatchCollection = reNumbers.Matches(strPhoneNumbers)
Dim strPhoneNumbersToExclude As String = ""
For Each mNumber As Match In mcNumbers
strPhoneNumbersToExclude = strPhoneNumbersToExclude & mNumber.Groups(0).Value & "|"
Next
strPhoneNumbersToExclude = Left(strPhoneNumbersToExclude, Len(strPhoneNumbersToExclude) - 1)
Dim matchpattern As String = "(?<=#PhoneNumber\r\n)""(?:(?!" & strPhoneNumbersToExclude & ")\d+)"""
Dim myEval As MatchEvaluator = New MatchEvaluator(AddressOf ReplaceMatch)
Dim swNewDataFile As New System.IO.StreamWriter("c:\reports\data.txt")
swNewDataFile.Write(Regex.Replace(strDataFile, matchpattern, myEval))
swNewDataFile.Close()
Console.WriteLine(intRepCount & " replacements, finished at " & Now())
End Sub
Public Function ReplaceMatch(ByVal m As Match) As String
Dim swLogFile As New System.IO.StreamWriter("c:\reports\replaced.log", True)
Dim strLogLine As String = "Replaced " & m.Groups(0).Value & " with " & strNewPhoneNumber & " " & Now()
Console.WriteLine(strLogLine)
swLogFile.WriteLine(strLogLine)
swLogFile.Close()
intRepCount = intRepCount + 1
Return strNewPhoneNumber
End Function
End Module
|