【发布时间】:2021-09-09 18:18:42
【问题描述】:
-bash-4.2$ python3 --version
Python 3.6.8
在网上找到这个程序来突出文本中的单词
#!/usr/bin/env python3
import re
class bcolors:
OK = '\033[92m' #GREEN
ACTIVE = '\033[92m' #GREEN
WARNING = '\033[93m' #YELLOW
FAIL = '\033[91m' #RED
ostkcd = '\033[91m' #RED
ERROR = '\033[91m' #RED
#COLOR = ['red', 'blue', 'orange', 'violet', 'green']
COLOR = [bcolors.OK, bcolors.FAIL,bcolors.WARNING,bcolors.ERROR,bcolors.ACTIVE]
text = """Graham says that Perl is cooler than Java and Python than Perl. In some circles, maybe. Graham uses the example of Slashdot, written in Perl. But what about Advogato, written in C? What about all of the cool P2P stuff being written in all three of the languages? Considering that Perl is older than Java, and was at one time the Next Big Language, I think you would have a hard time getting statistical evidence that programmers consider Perl "cooler" than Java, except perhaps by virtue of the fact that Java has spent a few years as the "industry standard" (and is thus uncool for the same reason that the Spice Girls are uncool) and Perl is still "underground" (and thus cool, for the same reason that ambient is cool). Python is even more "underground" than Perl (and thus cooler?). Maybe all Graham has demonstrated is that proximity to Lisp drives a language underground. Except that he's got the proximity to Lisp argument backwards too."""
regex = re.compile(r"(\blisp\b)|(\bpython\b)|(\bperl\b)|(\bjava\b)|(\bc\b)", re.I)
i = 0; output = " "
for m in regex.finditer(text):
output += "".join([text[i:m.start()],
"%s" % COLOR[m.lastindex-1],
text[m.start():m.end()],
" "])
i = m.end()
print ("".join([output, text[m.end():], " "]))
但它不单独打印高亮字。它突出显示第一个单词正则表达式出现到第二个正则表达式单词出现,依此类推。它突出了这一切  如何像这样仅突出显示正则表达式单词
【问题讨论】: