Lately I’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’s Twitter timeline. Here’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
{
public static string Link(this string s, string url)
{
return string.Format("<a href=\"{0}\" target=\"_blank\">{1}</a>", url, s);
}
public static string ParseURL(this string s)
{
return Regex.Replace(s, @"(http(s)?://)?([\w-]+\.)+[\w-]+(/\S\w[\w- ;,./?%&=]\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);
}
}
So as you can see I’m using the new Extension Methods feature in C# 3.0.
Now I can simply just call the extension methods like this:
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());
and the result should looks something like this:
Just blogged about how to parse html from the @twitter timeline – http://jesal.us/blog/?p=132 #programming
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’t want ParseURL to confuse those links with the original links present in the text.
This was inspired by Simon Whatley‘s post about doing something similar using prototyping with JavaScript.
ДА, вариант хороший
Change the Link method to return this and it works like a champ.
return string.Format(“{1}“, url, s);
Great post!
Ah, your blog is changing the return String.Format method. It just did it to my comment. Probably because it’s html… and it’s not encoding the comments.
It should be a href {0} for the url then {1} for the link name.
Thanks for pointing that out. I had to HTML encode those greater-than and less-than signs. Its a pain working with code snippets in WordPress.
письки
[...] utilizing a JavaScript equivalent of C# prototype methods which I had talked about in one of my earlier posts. Also, you’ll note that I’m using two different date/time jQuery plugins to make [...]
I like the helpful information you provide in your articles. I will bookmark your weblog and check again here frequently. I am quite certain I will learn lots of new stuff right here! Best of luck for the next!
I like this weblog so much, saved to my bookmarks .
Thanks for every other informative web site. The place else may just I get that kind of info written in such an ideal manner? I have a undertaking that I am just now operating on, and I’ve been on the glance out for such info.
I really enjoyed your article, I have also bookmarked your website and will check back soon. Thanks!
Great blog and the comments from people with other suggestions are all a wealth of information.
Excellent post. I was checking continuously this blog and I’m impressed! Very helpful info particularly the last part
I care for such info a lot. I was looking for this certain information for a long time. Thank you and best of luck.
Nice post. I was checking continuously this blog and I am impressed! Very useful information particularly the last part
I care for such information a lot. I was looking for this certain information for a long time. Thank you and good luck.
as I website owner I conceive the subject material here is really superb , regards for your efforts.
I enjoy gathering useful info, this post has got me even more info! .
Thanks for any other fantastic post. Where else may just anyone get that type of information in such an ideal means of writing? I have a presentation subsequent week, and I am on the look for such info.
Unusually good blog, lots of interesting tips I set here a liquid to their problems. Encounter with me and the the human race of casino gambling, you can carry off the palm a drawing lots!
Very good blog, lots of interesting tips I inaugurate here a solution to their problems. Meet with me and the faction of casino gambling, you can glean influence a raffle!
Thanks for the code. Ran into one minor issue. If a tweet had a decimal in it (aka: 1.2mg), it would convert that to a hyperlink. Had to swap out your regex for a different one that I found on regexlib.
(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?
Good to know!
Its like you read my mind! You appear to know so much about this, like you wrote the book in it or something. I think that you can do with a few pics to drive the message home a little bit, but instead of that, this is fantastic blog. A fantastic read. I’ll certainly be back.
Your home is valueble for me. Thanks!…
Some really interesting details you have written. Aided me a lot, just what I was looking for : D.
Appropriate on. It’s extra informative and straightforward to realize. Thanks a entire lot these a nice guideline.
[...] olay yaptığım bir projede lazım olmuştu, hazır yapılmışı varmı diye ararken burada ki kodları buldum. İş sadece biçimlendirmeyle bitmiyor, eğer Twitter’ da ki gibi ilgili [...]
That is really interesting, You are a very professional blogger. I’ve joined your rss feed and look ahead to in search of extra of your fantastic post. Additionally, I have shared your site in my social networks!
Hello there, just was aware of your blog thru Google, and found that it’s truly informative. I am going to be careful for brussels. I’ll appreciate should you proceed this in future. A lot of other folks will be benefited out of your writing. Cheers!
Thanks considerably with respect to the whole thing!
Dosięgnij fortunę – pokonaj ruletkę i oszukaj kasyno. Proste sposoby – gwarancja wygranej.
Haha what a interesting read, I really had quite a tingle reading this, It made my day enjoyable. I really think you should make more posts so we can understand the gripping storys in real life. I myself am starting my contagious own blog about topics I find that are emotional
I appreciate, cause I found just what I was looking forYou have ended my four day long hunt! God Bless you manHave a great dayBye
Cool story!
[...] Twitter Parser [...]