Created
March 10, 2016 16:57
-
-
Save alexgleason/5935726472c3823d1c45 to your computer and use it in GitHub Desktop.
Highlight search result match text in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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