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
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.
The second thing he didn't help me with, but that wouldn't be so hard i think.
Public Function Parse(ByVal logLine As String) As GameInfo
Dim info As New GameInfo
Dim rx As New Regex("(?<IP>(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}))(\:(?<port>(\d+)))?\t(?<wins>(\d+))\t(?<userName>((.|\s)*))\t(?<timestamp>(\d{10}))\t(?<points>(\d+))")
Public Function Parse(ByVal logLine As String) As GameInfo
Dim info As New GameInfo
Dim rx As New Regex("(?<IP>(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}))(\:(?<port>(\d+)))?\t(?<wins>(\d+))\t(?<userName>((.|\s)*))\t(?<timestamp>(\d{10}))\t(?<points>(\d+))")
just paste Class GameInfo after the end class in your form1 code.
also as i said you need the text file in the same folder as the executable, which is your projects bin/debug folder. don't forget to set your listview view to details + add 6 columns to it.