0

I'm trying to mimic the way that Google displays related search results.

Suppose I've searched for "widgets".

In the related terms Google presents, all words, except the search term are made bold.

The way they do it is to append the other bits with a <b> tag.

Here are some examples based on the search term widgets:

Term: blue widgets

<b>blue</b> widgets

Result: blue widgets

Term: widgets for cars

widgets <b>for cars</b>

Result: widgets for cars

Term: big widgets for real men with a love for widgets

<b>big</b widgets <b>for real men with a love for</b> widgets

Result: big widgets for real men with a love for widgets

I'm working in Asp.Net c#, and there's no reason why this shouldn't be done in code, not javascript - the issue is I can't think of a way to approach it!

Anyone done anything like this before and can offer some advice?

Thanks on advance.

John Ohara
  • 2,821
  • 3
  • 28
  • 54
  • Find the word and enclose it in a capture group, then just replace the group's value in a `` tag. [See this for reference](https://stackoverflow.com/a/52747318/7177029) – Kunal Mukherjee Apr 22 '19 at 07:45
  • Sorry Kunal, there are a few things I don't understand in your answer, such as "find the word" - how exactly, and "capture group". – John Ohara Apr 22 '19 at 07:50
  • Find the word as in literally use in the regex `Regex regex = new Regex(@"blue");` and enclose it in a [capturing group](https://www.regular-expressions.info/brackets.html) – Kunal Mukherjee Apr 22 '19 at 07:52
  • It's not quite as simple as you make out. Firstly, I'm looking for all words OTHER THAN "blue", then I need to encapsulate sections, not individual words. – John Ohara Apr 22 '19 at 07:55
  • Can you update your question to make it more clearer with some sample inputs and outputs – Kunal Mukherjee Apr 22 '19 at 07:56
  • What are you targetting: Winforms, WPF, ASP..? YOU should __always__ TAG your questions correctly so one can see it on the questions page! – TaW Apr 22 '19 at 07:58
  • In that case you can use negative look-ahead to find out all words which is not a word. Example [(?!widgets\b)\b\w+](https://regex101.com/r/TzIYmY/1) – Kunal Mukherjee Apr 22 '19 at 07:59
  • @KunalMukherjee - thanks for your answer. If you look at the output from the regex, it does find the 'negative keywords', but separately. This would mean all individual words were appended with tags rather than a string of words. – John Ohara Apr 22 '19 at 08:13
  • 1
    @TaW - noted and fixed. – John Ohara Apr 22 '19 at 08:14

1 Answers1

0

You can use negative lookahead to negate the word and get all negation followed by a Regex.Replace to replace it with enclosing <b> tags.

public static class Program
{
        private static void Main(string[] args)
        {

            string input = @"
            blue widgets

            widgets for cars

            big widgets for real men";

            string pattern = @"(?!widgets\b)\b\w+";

            string res = Regex.Replace(input, pattern, (match) =>
            {
                return $"<b>{match}</b>";
            });

            Console.WriteLine(res);
        }
    }
}

Outputting:

<b>blue</b> widgets

widgets <b>for</b> <b>cars</b>

<b>big</b> widgets <b>for</b> <b>real</b> <b>men</b>
Kunal Mukherjee
  • 5,775
  • 3
  • 25
  • 53
  • Thanks for the thorough example Kunal, I really appreciate it. As I pointed out in my last comment, the regex selects every individual word - is there any adjustment that could me made so that strings of adjacent keywords are within the same - for instance, for real men – John Ohara Apr 22 '19 at 08:28
  • Kunal, one again, thanks for your input. I've tested the code above but it fails in two areas. The first I've already covered above, but more importantly, it doesn't work with multi-word phrases - only single words. – John Ohara Apr 22 '19 at 09:23