不过,我正试图通过QRegExp类在PyQt4中实现以下目标。
我很难找到关于如何在python中使用这个类的好例子。
def html_trap(text):
h ={"&":"&",'"':""","'":"'",">":">","<":"<"}
t=""
for key,value in h.items():
m=re.search(value,text)
if m is not None:
t=text.replace(value,key)
return t
print(html_trap("Grocery " Gourmet Food"))谢谢
发布于 2018-10-18 16:06:50
您必须使用search()而不是搜索,您必须使用indexIn(),这将返回查找元素的位置或-1 (如果找不到)。
from PyQt4 import QtCore
def html_trap(text):
h ={"&": "&",'"':""","'":"'",">":">","<":"<"}
t=""
for key, value in h.items():
regex = QtCore.QRegExp(value)
if regex.indexIn(text) != -1:
t = text.replace(value, key)
return t
print(html_trap("Grocery " Gourmet Food"))输出:
Grocery " Gourmet Foodhttps://stackoverflow.com/questions/52877595
复制相似问题