We help IT Professionals succeed at work.
Get Started

search results highlighting/regexp-voodoo

LeeOMara
LeeOMara asked
on
290 Views
Last Modified: 2008-03-06
PROBLEM:
I want to produce short "snippets" for search result pages. The search results are already partially processed (search terms get wrapped in [strong] tags), but I keep hitting dead-ends with this step. Now, I've tried a wide number of approaches, spent an embarasing amount of time and arrived at no viable solution. Learning regular expressions would probably have been less painfull, but there you have it.

REQUIREMENTS:
- the snippet should be made up of five words on either side of the search terms.
- end product cannot have any unclosed/unmatched tags.

SETUP:
- Search terms are (already) marked up in [strong] tags with a class attribute of term1 - term3.
- No other tags exist in the string (other than aforementioned [strong] tags)

EXAMPLE INPUT:
The journalist/critic Edmund Gosse begins championing <strong class="term3">Ibsen</strong> in English periodicals. His article "<strong class="term3">Ibsen</strong> the Norwegian Satirist" (Fortnightly Review, January 1873) is later expanded in his book Studies in the Literature of Northern Europe (1879). In contrast, the conservative dramatic critic Clement W. Scott begins writing columns for <strong class="term1">London</strong>'s Daily Telegraph; he comes to consider the influence of <strong class="term3">Ibsen</strong> the worst thing that ever happened to English drama. His hysterically negative attack on <strong class="term3">Ibsen's</strong> Ghosts in 1891 will be paraphrased (and reduced to ridicule) by <strong class="term2">Shaw</strong> in the first chapter of The Quintessence of <strong class="term3">Ibsen</strong>ism (1891).

EXAMPLE OUTPUT:
... journalist/critic Edmund Gosse begins championing <strong class="term3">Ibsen</strong> in English periodicals. His article ... Scott begins writing columns for <strong class="term1">London</strong>'s Daily Telegraph; he comes to consider ... (and reduced to ridicule) by <strong class="term2">Shaw</strong> in the first chapter of The Quintessence...
Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 6 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE