' Collection of code snippets by Arne Vajhøj 
' (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
Imports System
Imports System.Text
Imports System.Text.RegularExpressions

Public Class HtmlManip
    Public Shared Sub Main(ByVal args As String())
        Test("[url]http://www.exp.dk[/url]")
        Test("[url=http://www.exp.dk]Tryk Her[/url]")
        Test("bla [url]http://www.exp.dk[/url] bla [url=http://www.exp.dk]Tryk Her[/url] bla")
    End Sub

    Private Shared Sub Test(ByVal s As String)
        Console.WriteLine(s)
        Console.WriteLine(LinkCreate(s))
    End Sub

    Public Shared Function LinkCreate(ByVal s As String) As String
        Return LinkCreate2(LinkCreate1(s))
    End Function

    Public Shared Function LinkCreate1(ByVal s As String) As String
        Dim m As Match = Regex.Match(s, "(.*?)(?:\[url\])(.*?)(?:\[/url\])(.*)", RegexOptions.Singleline)
        If m Is Match.Empty Then
            Return s
        Else
            Return m.Groups(1).ToString & "<a href='" & m.Groups(2).ToString & "'>" & m.Groups(2).ToString + "</a>" & LinkCreate(m.Groups(3).ToString)
        End If
    End Function

    Public Shared Function LinkCreate2(ByVal s As String) As String
        Dim m As Match = Regex.Match(s, "(.*?)(?:\[url=)(.*?)(?:\])(.*?)(?:\[/url\])(.*)", RegexOptions.Singleline)
        If m Is Match.Empty Then
            Return s
        Else
            Return m.Groups(1).ToString & "<a href='" & m.Groups(2).ToString & "'>" & m.Groups(3).ToString + "</a>" & LinkCreate(m.Groups(4).ToString)
        End If
    End Function
End Class 
