python truncate text around keyword -


i have string , want search keyword or phrase , return portion of text before , after keyword or phrase. google talking about.

here string grabbed web:

"this filter truncates words original truncate words django filter, instead of being based on number of words, it's based on number of characters. found need when building website i'd have show labels on small text boxes , truncating words didn't gave me best results (and truncating character is...well...not elegant)."

now lets want search phrase building website , output this:

"... need when building website i'd have show ..."

edit: should have made more clear. has work on multiple strings / phrases, not one.

building on answers of others (especially cababunga's) function, take 25 (or many) characters, stopping @ last word boundary, , provide nice match:

import re  def find_with_context(haystack, needle, context_length, escape=true):     if escape:         needle = re.escape(needle)     return re.findall(r'\b(.{,%d})\b(%s)\b(.{,%d})\b' % (context_length, needle, context_length), haystack)  # returns list of three-tuples, (context before, match, context after). 

usage:

>>> find_with_context(s, 'building website', 25) [(' need when ', 'building website', " i'd have show ")] >>> # compare without making sure ends @ word boundaries: ... # [('d need when ', 'building website', " i'd have show l")] ... >>> match in find_with_context(s, 'building website', 25): ...     print '<p>...%s<strong>%s</strong>%s...</p>' % match ...  <p>... need when <strong>building website</strong> i'd have show ...</p> 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -