// 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)
using System;
using System.Text;
using System.Text.RegularExpressions;
public class Links
{
public static void Main(string[] args)
{
Test("[link]http://www.exp.dk[/link]");
Test("[link=http://www.exp.dk]Tryk Her[/link]");
Test("bla [link]http://www.exp.dk[/link] bla [link=http://www.exp.dk]Tryk Her[/link] bla");
}
private static void Test(string s)
{
Console.WriteLine(s);
Console.WriteLine(LinkCreate(s));
}
public static string LinkCreate(string s)
{
return LinkCreate2(LinkCreate1(s));
}
public static string LinkCreate1(string s)
{
Match m = Regex.Match(s, "(.*?)(?:\\[link\\])(.*?)(?:\\[/link\\])(.*)", RegexOptions.Singleline);
if (m == Match.Empty) {
return s;
} else {
return m.Groups[1].ToString() + "" + m.Groups[2].ToString() + "" + LinkCreate(m.Groups[3].ToString());
}
}
public static string LinkCreate2(string s)
{
Match m = Regex.Match(s, "(.*?)(?:\\[link=)(.*?)(?:\\])(.*?)(?:\\[/link\\])(.*)", RegexOptions.Singleline);
if (m == Match.Empty) {
return s;
} else {
return m.Groups[1].ToString() + "" + m.Groups[3].ToString() + "" + LinkCreate(m.Groups[4].ToString());
}
}
}