I’m using following method to highlight the keywords in a given text.
private string HighlightSearchKeyWords(string searchKeyWord, string text)
{
Regex keywordExp = new Regex(@" ?, ?");
var pattern = @"\b(" + keywordExp.Replace(Regex.Escape(searchKeyWord), @"|") + @")\b";
Regex exp = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Singleline);
return exp.Replace(text, @"<span class=""search-highlight"">$0</span>");
}
Sample Text: “What is .net Programming? Pl suggest few e-books”
Keyword: “.net”
When i try to search with key word “.net” .net is not getting highlighted in the given sample text.
When i try to search with key word “e-books” e-books is getting highlighted in the given sample text.
What would be the problem. Can anyone pl let me know where exactly do i need modify/
There is no word boundary before “.net” because
\bonly looks for a change between\wand\W, and both.and(space) fall into the\Wcategory, so there is no boundary between them.One option is to simply look for “not a word-character” – i.e. not explicitly checking for the boundary, only for the lack of a word character, using a negative lookbehind:
You could also check for anything that’s not non-whitespace character, like so:
This one is a double-negative – it might seem more obvious to do
(?<=\s)(or(?<=\W)for previous example), but these will prevent matches at the start of line from matching.For an example of the difference between these two – the first one would match the .NET in
C#.NETwhilst the second one would not.Since you’re using .NET regex, you’ve fortunately got a fairly complete set of regex functionality – but it’s worth point out that some other regex implementations don’t support negative lookbehind – for those, you would need to use syntax like this:
(In all these cases, you want the equivalent lookahead on the other end.)
So, here’s how those four variants would look in your pattern: