One of the things I need frequently is to take text entered and expand URLs in it. There’s code in my library that handles this via an ExpandUrls class that does this:
public class ExpandUrlsParser
{
public string Target = "";
/// <summary>
/// Expands links into HTML hyperlinks inside of text or HTML.
/// </summary>
/// <param name="Text">The text to expand</param>
/// <param name="Target">Target frame where output is displayed</param>
/// <returns></returns>
public string ExpandUrls(string Text)
{
string pattern = @"[""'=]?(http://|ftp://|https://|www\.|ftp\.[\w]+)([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])";
// *** Expand embedded hyperlinks
System.Text.RegularExpressions.RegexOptions options =
RegexOptions.IgnorePatternWhitespace |
RegexOptions.Multiline |
RegexOptions.IgnoreCase;
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(pattern, options);
MatchEvaluator MatchEval = new MatchEvaluator(this.ExpandUrlsRegExEvaluator);
return Regex.Replace(Text, pattern, MatchEval);
}
/// <summary>
/// Internal RegExEvaluator callback. Expands the URL
/// </summary>
/// <param name="M"></param>
/// <returns></returns>
private string ExpandUrlsRegExEvaluator(System.Text.RegularExpressions.Match M)
{
string Href = M.Value; // M.Groups[0].Value;
// *** if string starts within an HREF don't expand it
if (Href.StartsWith("=") ||
Href.StartsWith("'") ||
Href.StartsWith("\""))
return Href;
string Text = Href;
if (Href.IndexOf("://") < 0)
{
if (Href.StartsWith("www."))
Href = "http://" + Href;
else if (Href.StartsWith("ftp"))
Href = "ftp://" + Href;
else if (Href.IndexOf("@") > -1)
Href = "mailto:" + Href;
}
string Targ = !string.IsNullOrEmpty(this.Target) ? " target='" + this.Target + "'" : "";
return "<a href='" + Href + "'" + Targ +
">" + Text + "</a>";
}
}
This class basically takes a string as input and then spits out a formatted document that expands all URLs in the document skipping over any already expanded URLs etc. It can be used like this:
protected void Page_Load(object sender, EventArgs e)
{
ExpandUrlsParser Parser = new ExpandUrlsParser();
Parser.Target = Target;
string Result = Parser.ExpandUrls("this is a test for www.asp.net expressions. http://www.west-wind.com. www.west-wind.NET");
Response.Write( Result);
return;
}
Or a little easier with a wrapper function in my utility class:
/// <summary>
/// Expands links into HTML hyperlinks inside of text or HTML.
/// </summary>
/// <param name="Text">The text to expand</param>
/// <param name="Target">Target frame where output is displayed</param>
/// <returns></returns>
public static string ExpandUrls(string Text, string Target)
{
ExpandUrlsParser Parser = new ExpandUrlsParser();
Parser.Target = Target;
return Parser.ExpandUrls(Text);
}
I’ve been using this parser for quite some time, but there is one bug that’s been eluding me that bonks on .net extensions. As it turns out I was missing a closing bracket and that fixed trapping the .net extension properly, but now I’ve run into another issue...
So as I was mucking around with the expression I ran into another odd bug where the parser for whatever reason was not picking up the IgnoreCase option and failing on upper case extensions. I went back and forth checking my code, chekcing the expressions in various different RegEx parsers including my favorite RegexBuddy and Roy Osherove's Regulator (which is built in .NET so it uses the same parser your code is using). Nothing couldn’t get it to run...
As it turns out in my over eagerness I missed actually APPLYING the damn options to the Regex.Replace() call... Talk about feeling like a dolt and losing an hour that I’ll never get back <g>...
For me that’s exactly the problem with RegEx expressions. If something – even the simplest thing – goes wrong with a RegEx expression I’ll be some time trying to re-figure out how the damn expression works and by then my eyes are so cross-eyed I can’t see the forest for the trees anymore <g>...
Actually it looks like the code I had previously didn’t apply these flags either so at least that particular bug has been fixed... Not all wasted time at least...
Other Posts you might also like