How might I highlight the matches in regex in the sentence? I figure that I could use the locations of the matches like I would get from this:
s = "This is a sentence where I talk about interesting stuff like sencha tea."
spans = [m.span() for m in re.finditer(r'sen\w+', s)]
But how do I force the terminal to change the colors of those spans during the output of that string?
There are several terminal color packages available such as termstyle or termcolor. I like colorama, which works on Windows as well.
Here's an example of doing what you want with colorama:
from colorama import init, Fore
import re
init() # only necessary on Windows
s = "This is a sentence where I talk about interesting stuff like sencha tea."
print(re.sub(r'(sen\w+)', Fore.RED + r'\1' + Fore.RESET, s))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With