Displaying "Google like" search results
Working in ASP.NET (VB), I am trying to develop a simple search results page for my website.
The process is as follows:
The site's 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 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 ret开发者_如何转开发urn 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....
Google displays a sentence in which keyword was found. Assuming you already found a keyword position in a text, I would do:
- Go backwards char by char from keyword position until you find
.
or?
or!
or beginning of a text. - Return a substring of required length from that position.
(\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
精彩评论