Regex-noob here. Looking for some C# regex code to “syntax highlight” twitter text. So given this tweet:
@taglius here's some tweet text that shouldn't be highlighted #tagtestpix http://aurl.jpg
I want to find the user mentions (@), hashtags (#), and urls (http://) and add appropriate html to color highlight these elements. Something like
<font color=red>@taglius</font> here's some tweet text that shouldn't be highlighted <font color=blue>#tagtestpix</font> <font color=yellow>http://aurl.jpg</font>
This isn’t the exact html I will use, but I think you get the idea.
The following would match the ‘@’ character followed by a sequence of alpha-num characters:
The following would match the ‘#’ character followed by a sequence of alpha-num characters:
There are a lot of free-form http url match expressions, this is the one I use most commonly:
Lastly, You’re going to get false positive hits with all of these so you’re going to need to look real hard at how to correctly delineate these tags… For instance you have the following tweet:
Obviously this is going to be a problem as all three of the expressions will match inside the url. To avoid this you will need to figure out what characters are allowed to precede or follow the match. As an example, the following requires a whitespace or start of string to precede the @name reference and requires a ‘,’ or space following it.
Regex patterns are not easy, I recommend getting a tool like Expresso.