<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>jesal gadhia &#187; twitter</title>
	<atom:link href="http://jesal.us/tag/twitter/feed/" rel="self" type="application/rss+xml" />
	<link>http://jesal.us</link>
	<description></description>
	<lastBuildDate>Thu, 14 Jul 2011 18:51:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>How to Parse Twitter Usernames, Hashtags and URLs in C# 3.0</title>
		<link>http://jesal.us/2009/05/how-to-parse-twitter-usernames-hashtags-and-urls-in-c-30/</link>
		<comments>http://jesal.us/2009/05/how-to-parse-twitter-usernames-hashtags-and-urls-in-c-30/#comments</comments>
		<pubDate>Wed, 06 May 2009 22:13:27 +0000</pubDate>
		<dc:creator>J</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://jesal.us/blog/?p=132</guid>
		<description><![CDATA[Lately I&#8217;ve been working on my pet project called Twime. So as part of that project, I wanted to add the ability to parse URLs, usernames and hashtags from the user&#8217;s Twitter timeline. Here&#8217;s how I went about doing that: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Text.RegularExpressions; public static class HTMLParser { [...]]]></description>
			<content:encoded><![CDATA[<p>Lately I&#8217;ve been working on my pet project called <a href="http://jesal.us/twime/" target="_blank">Twime</a>. So as part of that project, I wanted to add the ability to parse URLs, usernames and hashtags from the user&#8217;s Twitter timeline. Here&#8217;s how I went about doing that:</p>
<pre name="code" class="c-sharp:nogutter;">
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;

public static class HTMLParser
{
    public static string Link(this string s, string url)
    {
        return string.Format("&lt;a href=\"{0}\" target=\"_blank\"&gt;{1}&lt;/a&gt;", url, s);
    }
    public static string ParseURL(this string s)
    {
        return Regex.Replace(s, @"(http(s)?://)?([\w-]+\.)+[\w-]+(/\S\w[\w- ;,./?%&#038;=]\S*)?", new MatchEvaluator(HTMLParser.URL));
    }
    public static string ParseUsername(this string s)
    {
        return Regex.Replace(s, "(@)((?:[A-Za-z0-9-_]*))", new MatchEvaluator(HTMLParser.Username));
    }
    public static string ParseHashtag(this string s)
    {
        return Regex.Replace(s, "(#)((?:[A-Za-z0-9-_]*))", new MatchEvaluator(HTMLParser.Hashtag));
    }
    private static string Hashtag(Match m)
    {
        string x = m.ToString();
        string tag = x.Replace("#", "%23");
        return x.Link("http://search.twitter.com/search?q=" + tag);
    }
    private static string Username(Match m)
    {
        string x = m.ToString();
        string username = x.Replace("@", "");
        return x.Link("http://twitter.com/" + username);
    }
    private static string URL(Match m)
    {
        string x = m.ToString();
        return x.Link(x);
    }
}
</pre>
<p>So as you can see I&#8217;m using the new <a href="http://msdn.microsoft.com/en-us/library/bb383977.aspx" target="_blank">Extension Methods</a> feature in C# 3.0.</p>
<p>Now I can simply just call the extension methods like this:</p>
<pre name="code" class="c-sharp:nogutter;">
string tweet = "Just blogged about how to parse HTML from the @twitter timeline - http://jesal.us/blog/?p=132 #programming";
Response.Write(tweet.ParseURL().ParseUsername().ParseHashtag());
</pre>
<p>and the result should looks something like this:</p>
<p>Just blogged about how to parse html from the <a href="http://twitter.com/twitter" target="_blank">@twitter</a> timeline &#8211; <a href="http://jesal.us/blog/?p=132" target="_blank">http://jesal.us/blog/?p=132</a> <a href="http://search.twitter.com/search?q=%23programming">#programming</a></p>
<p>Just be sure to call ParseURL method before ParseUsername and ParseHashtag. The other two methods will add URLs to the usernames and hastags and you don&#8217;t want ParseURL to confuse those links with the original links present in the text.</p>
<p>This was inspired by <a href="http://www.simonwhatley.co.uk/" target="_blank">Simon Whatley</a>&#8216;s <a href="http://www.simonwhatley.co.uk/parsing-twitter-usernames-hashtags-and-urls-with-javascript" target="_blank">post</a> about doing something similar using prototyping with JavaScript.</p>
]]></content:encoded>
			<wfw:commentRss>http://jesal.us/2009/05/how-to-parse-twitter-usernames-hashtags-and-urls-in-c-30/feed/</wfw:commentRss>
		<slash:comments>33</slash:comments>
		</item>
	</channel>
</rss>

