I have got this code from a friend:
I will use it for my project. What i want it to do is reading a txt file and read line by line and use this function to separate the lines into specific data. And the it should add a new item to listview detailed view for each line.Code:Imports System.Text.RegularExpressions Public Class GameInfo Private Const NumberOfFields As Integer = 5 ' Use properties in real code; I'm using fields for brevity. Public IpAddress As String Public Port As Integer Public Wins As Integer Public UserName As String Public TimeStamp As Integer Public Points As Integer Public Shared Function Parse(ByVal logLine As String) As GameInfo Dim info As New GameInfo Dim pattern As String = "(?<IP>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(?<port>\d+) (?<wins>\d+) (?<userName>.*) (?<timestamp>\d{10}) (?<points>\d+)" Dim lineMatch As Match = Regex.Match(logLine, pattern) If Not lineMatch.Success Then ' Invalid line, possibly return an error Return info End If ' Group 0 is always the entire capture If lineMatch.Groups.Count <> NumberOfFields + 1 Then ' Invalid line, possibly return an error Return info End If info.IpAddress = lineMatch.Groups("IP").Value info.Port = CInt(lineMatch.Groups("port").Value) info.Wins = CInt(lineMatch.Groups("wins").Value) info.UserName = lineMatch.Groups("userName").Value info.TimeStamp = CInt (lineMatch.Groups("timestamp").Value info.Points = CInt(lineMatch.Groups("points").Value) Return info End Function Public Overrides Function ToString() As String Return String.Format("{0} ({1}:{2}) had {3} wins and {4} points.", _ UserName, IpAddress, Port, Wins, Points) End Function End Class
The second thing he didn't help me with, but that wouldn't be so hard i think.
Hope someone will look into this.




Reply With Quote