Highlighting keywords in text using Regex.Replace (Perfect for SEO)
Why
I needed to take some text and bold certain keywords before returning the data to the web browser to enhance my Search Engine Optimization
Example
The following example shows how I achieved this although it does contain dummy data. I created a new C# 2005 Console App and added the following to the Main method:
string keywords = "Cat, rabbit, dog,hound, fox";
string text = "The cat spoke to the dog and told him what the rabbit did to the fox while the hound was sleeping.";
Console.WriteLine(HighlightKeywords(keywords, text));
Console.ReadLine();
Then added the follwoing static methods:
private static string HighlightKeywords(string keywords, string text)
{
// Swap out the ,<space> for pipes and add the braces
Regex r = new Regex(@", ?");
keywords = "(" + r.Replace(keywords, @"|") + ")";
// Get ready to replace the keywords
r = new Regex(keywords, RegexOptions.Singleline | RegexOptions.IgnoreCase);
// Do the replace
return r.Replace(text, new MatchEvaluator(MatchEval));
}
private static string MatchEval(Match match)
{
if (match.Groups[1].Success)
{
return "<b>" + match.ToString() + "</b>";
}
return ""; //no match
}
Result
The <b>cat</b> spoke to the <b>dog</b> and told him
what the <b>rabbit</b> did to the <b>fox</b> while
the <b>hound</b> was sleeping.
Explanation
First of all, I needed to swap out the comma+space (or just comma in some cases) for the pipe character '(regex or)'
// Swap out the ,<space> for pipes and add the braces
Regex r = new Regex(@", ?");
keywords = "(" + r.Replace(keywords, @"|") + ")";
I then prepared a Regex object for the main keyword replace:
// Get ready to replace the keywords
r = new Regex(keywords, RegexOptions.Singleline | RegexOptions.IgnoreCase);
You can see I chose to ignore case and match based on Singleline. Singleline ignores new line characters mid match.. for example two words seperated by newline rather than space.
Now comes the replace. You'll notice that I pass a MatchEvaluator into the replace method. I use this to choose what to replace the match with.
// Do the replace
return r.Replace(text, new MatchEvaluator(MatchEval));
The MatchEval method only looks for the first group match, that's all I need. Had the master regular expresion contained two groups, the MatchEval method would have required a second If.
| Author |
: Paul Hayman |
| Published |
: 20 July 2006 |
Paul is the COO of kwiboo ltd consultant and has more than a decade of IT consultancy experience. He has consulted for a number of blue chip companies and has been exposed to the folowing sectors: Utilities, Telecommunications, Insurance, Media, Investment Banking, Leisure, Legal, CRM, Pharmaceuticals, Interactive Gaming, Mobile Communications, Online Services.
Paul is the COO and co-founder of kwiboo (http://www.kwiboo.com/) and is also the creator of GeekZilla.