views:

79

answers:

2

Working in ASP.NET (VB), I am trying to develop a simple search results page for my website.

The process is as follows:

(1) The site's user enters a search phrase;

(2) 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 the search phrase highlighted.

I already have the search part done, and also the "highlighted" part done (using Regex). However, I want to be able to return a short snippet of text, which include the search phrase (a few words before the search phrase, a few after). Something like:

Page Title [as a link]

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

A: 

Google displays a sentence in which keyword was found. Assuming you already found a keyword position in a text, I would do:

  1. Go backwards char by char from keyword position until you find . or ? or ! or beginning of a text.
  2. Return a substring of required length from that position.
serg
A: 
(\b\S+\b(\s*)){3}search phrase((\s*)\b\S+\b){3}

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

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

Thexa4