Displaying search results "Google like"

Working in ASP.NET (VB) I am trying to create a simple search results page for my site.

The process looks like this:

  • The site user enters a search phrase;

  • The search results page searches the site's database, returns the page title as a link, and a short snippet from each search hit with a highlighted search phrase.

I already have the search part as well as the highlighted part (using Regex). However, I want to be able to return a short piece of text that includes the search phrase (a few words before the search phrase, a few after). Sort of:

Page title [as a link]

... yada yada yada search phrase yada yada yada ....

+2


a source to share


2 answers


Google displays the offer in which the keyword was found. Assuming you already found the keyword in the text, I would do:



  • Backtrack char by char from the keyword position until you find .

    either ?

    or !

    or the beginning of the text.
  • Returns a substring of the required length from this position.
+1


a source


(\b\S+\b(\s*)){3}search phrase((\s*)\b\S+\b){3}

      

This will select 3 words before the phrase "search phrase" and 3 words after.



If you have the phrase "lorum ipsum search phrase" it will probably only match the first search phrase

+1


a source







All Articles