简体   繁体   English

QregExp类PyQt4

[英]QregExp class PyQt4

I am trying to achieve the following, however with the QRegExp class in PyQt4. 我正在尝试通过PyQt4中的QRegExp类实现以下目标。

I am struggling to find good examples on how to use this class in python. 我正在努力寻找有关如何在python中使用此类的良好示例。

def html_trap(text):
    h ={"&":"&amp;",'"':"&quot;","'":"&apos;",">":"&gt;","<":"&lt;"}
    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 &quot; Gourmet Food"))

Thanks 谢谢

Instead of search you must use search() you must use indexIn() , this returns the position of the found element or -1 if you can not find it 代替搜索,必须使用search() ,必须使用indexIn() ,这将返回找到的元素的位置;如果找不到,则返回-1

from PyQt4 import QtCore


def html_trap(text):
    h ={"&": "&amp;",'"':"&quot;","'":"&apos;",">":"&gt;","<":"&lt;"}
    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 &quot; Gourmet Food"))

Output: 输出:

Grocery " Gourmet Food

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM