I have list of keywords (single word or couple of words) which I want to replace with some URLs.
Like:
London will be replaced with
<a href="http://www.mysite/london-events/london">London</a>Football events in London with
<a href="http://www.mysite/footbal-events/london"> Football events in London</a>London footbal events with
<a href="http://www.mysite/footbal-events/london"> London football events</a>Football events london with
<a href="http://www.mysite/footbal-events/london"> Football events London</a>Party sites in london with
<a href="http://www.mysite/party-sites/london"> party sites in London</a>London party sites with
<a href="http://www.mysite/party-sites/london"> London party sites</a>
I put above key/values in Dictionary, keywords in key and URLs in value and replaced like
Contents is as follow:
London is a great city and have football events in london but party sites in london are also good. London football events are great along with London party sites. Enjoy London!
Code to replace key/values:
private static string ParsedContents(some arguments list here...)
{
Dictionary<string, string> keyWords = GetKeywordsAndEntityWithURL(some arguments list here...);
StringBuilder parsedContents = new StringBuilder(contents);
foreach (var keyWord in keyWords)
{
string replacedString = Regex.Replace(parsedContents.ToString(), "\\b" + keyWord.Key + "\\b", keyWord.Value, RegexOptions.IgnoreCase);
parsedContents.Remove(0, parsedContents.Length);
parsedContents.Append(replacedString);
}
// retrun parsed contents as string.
return parsedContents.ToString();
}
When I run my code only 'London' replaced with '<a href="http://www.mysite/london-events/london">London</a>' and all other just remain the same but if I remove 'London' from keywords it works fine.
Can you please help me out that how I can match whole string.
Contents to replace and urls are fake:
Thanks