This is a simple snippet which takes raw tweet text and converts it into a formatted tweet. Formatted tweet contains links to user handles, Links and Hashtags
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private string ParseTweet(string rawTweet) | |
{ | |
Regex link = new Regex(@"http(s)?://([\w+?\.\w+])+([a-zA-Z0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?"); | |
Regex screenName = new Regex(@"@\w+"); | |
Regex hashTag = new Regex(@"#\w+"); | |
string formattedTweet = link.Replace(rawTweet, delegate(Match m) | |
{ | |
string val = m.Value; | |
return "<a href='" + val + "'>" + val + "</a>"; | |
}); | |
formattedTweet = screenName.Replace(formattedTweet, delegate(Match m) | |
{ | |
string val = m.Value.Trim('@'); | |
return string.Format("@<a href='http://twitter.com/{0}'>{1}</a>", val, val); | |
}); | |
formattedTweet = hashTag.Replace(formattedTweet, delegate(Match m) | |
{ | |
string val = m.Value; | |
return string.Format("<a href='http://twitter.com/#search?q=%23{0}'>{1}</a>", val, val); | |
}); | |
return formattedTweet; | |
} |
Any idea what the regex would be to ignore email addresses whilst hyperlinking Twitter usernames?
ReplyDeleteThis comment has been removed by the author.
ReplyDeletePerfect! Works like a charm. Thanks!
ReplyDeleteThanks works great.
ReplyDelete