正则表达式python优化或idea文本高亮显示
我有下面的正则表达式,我需要一些关于它的建议。我需要建议如何在不改变单词形式的情况下突出显示文本(大写保持大写)。我有一个我想强调的单词列表,因此我得到了以下内容:正则表达式python优化或idea文本高亮显示,python,regex,optimization,Python,Regex,Optimization,我有下面的正则表达式,我需要一些关于它的建议。我需要建议如何在不改变单词形式的情况下突出显示文本(大写保持大写)。我有一个我想强调的单词列表,因此我得到了以下内容: def tagText(self,listSearch,docText): docText=docText.decode('utf-8') for value in listSearch: replace = re.compile(ur""+value+"", flags=re.IGNO
def tagText(self,listSearch,docText):
docText=docText.decode('utf-8')
for value in listSearch:
replace = re.compile(ur""+value+"", flags=re.IGNORECASE | re.UNICODE)
docText = replace.sub(u"""<b style="color:red">"""+value+"""</b>""", docText, re.IGNORECASE | re.UNICODE)
return docText
def标记文本(self、listSearch、docText):
docText=docText.decode('utf-8')
对于listSearch中的值:
replace=re.compile(ur”“+value+”,flags=re.IGNORECASE | re.UNICODE)
docText=replace.sub(u+value+),docText,re.IGNORECASE|re.UNICODE)
返回docText
您需要在替换字符串中使用占位符,而不是文字值
def tag_text(self, items, text):
text = text.decode('utf-8')
for item in items:
text = re.sub(
re.escape(item),
ur'<b style="color:red">\g<0></b>',
text,
flags=re.IGNORECASE | re.UNICODE)
return text
print tag_text(None, ["foo", "bar"], "text with Foo and BAR")
# text with <b style="color:red">Foo</b> and <b style="color:red">BAR</b>
def tag_文本(自身、项目、文本):
text=text.decode('utf-8')
对于项目中的项目:
text=re.sub(
关于逃生(项目),
ur'\g',
文本,
flags=re.IGNORECASE | re.UNICODE)
返回文本
打印标签文本(无、[“foo”、“bar”]、“带foo和bar的文本”)
#带有Foo和BAR的文本
(我还对函数进行了一些清理,使其看起来更“pythonic”)。如果使用HTML解析器替换标记,则不会出现此问题。不要使用正则表达式来解析HTML。小马托尼会毁了我们所有人!可能重复的不是它不是html解析器。