C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Tip: For Groups, make sure to access the group at index 1 for the first group. We do not access the first element at index 0.
VB.NET program that uses Regex on lines in file
Imports System.IO
Imports System.Text.RegularExpressions
Module Module1
Sub Main()
' Create Regex.
Dim regex As Regex = New Regex("\s/Content/([a-zA-Z0-9\-]+?)\.aspx")
' Loop over the lines in this text file.
Using reader As StreamReader = New StreamReader("C:\programs\log.txt")
While (True)
Dim line As String = reader.ReadLine()
' End if Nothing.
If line = Nothing Then
Return
End If
' Try to match.
Dim match As Match = regex.Match(line)
If match.Success Then
' Get first group and print it.
Dim value As String = match.Groups(1).Value
Console.WriteLine(line)
Console.WriteLine("... " + value)
End If
End While
End Using
End Sub
End Module
File contents, log.txt:
2008-10-16 23:56:44 W3SVC2915713 GET /Content/String.aspx - 80 66.249
2008-10-16 23:59:50 W3SVC2915713 GET /Content/Trim-String-Regex.aspx - 80 66.249
Output
2008-10-16 23:56:44 W3SVC2915713 GET /Content/String.aspx - 80 66.249
... String
2008-10-16 23:59:50 W3SVC2915713 GET /Content/Trim-String-Regex.aspx - 80 66.249
... Trim-String-Regex