For colored text, you can use ANSI cancel codes. In python, you should do the following to change the color of the text from now on.
print '\033[' + str(code) + 'm'
Here the code is the meaning here . Please note that 0 will reset any changes, and 30-37 will reset. So basically you want to insert '\ 033 [' + str (code) + 'm' before the match and '\ 033 [0m' after that to reset your terminal. For example, the following should print all terminal colors you print:
print 'break'.join('\033[{0}mcolour\33[0m'.format(i) for i in range(30, 38))
Below is a dirty example of what you requested
import re colourFormat = '\033[{0}m' colourStr = colourFormat.format(32) resetStr = colourFormat.format(0) s = "This is a sentence where I talk about interesting stuff like sencha tea." lastMatch = 0 formattedText = '' for match in re.finditer(r'sen\w+', s): start, end = match.span() formattedText += s[lastMatch: start] formattedText += colourStr formattedText += s[start: end] formattedText += resetStr lastMatch = end formattedText += s[lastMatch:] print formattedText
source share