Skip to content

Instantly share code, notes, and snippets.

@alexgleason
Created March 10, 2016 16:57
Show Gist options
  • Save alexgleason/5935726472c3823d1c45 to your computer and use it in GitHub Desktop.
Save alexgleason/5935726472c3823d1c45 to your computer and use it in GitHub Desktop.
Highlight search result match text in Python
import re
def highlight_matches(query, text):
def span_matches(match):
html = '<span class="query">{0}</span>'
return html.format(match.group(0))
return re.sub(query, span_matches, text, flags=re.I)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment