Welcome to 16892 Developer Community-Open, Learning,Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

怎么回事,查找只匹配第一个空格前的部分,我希望不用正则全部匹配?

def btnfind_fun(self):
    pattern = self.codeline.text()
    if len(pattern)>0 and pattern!=' ' :
        cursor = self.CodetextEdit.textCursor()
        # Setup the desired format for matches
        format = QtGui.QTextCharFormat()
        format.setBackground(QtGui.QBrush(QtGui.QColor("red")))
        # Setup the regex engine
        regex = QRegExp(pattern,QtCore.Qt.CaseInsensitive)
        # Process the displayed document
        pos = 0
        index = regex.indexIn(self.CodetextEdit.toPlainText(), pos)
        while (index != -1):
            # Select the matched text and apply the desired format
            cursor.setPosition(index)
            cursor.movePosition(QtGui.QTextCursor.EndOfWord, 1)
            cursor.mergeCharFormat(format)
            # Move to the next match
            pos = index + regex.matchedLength()
            index = regex.indexIn(self.CodetextEdit.toPlainText(), pos)
        self.CodetextEdit.moveCursor(pos)![image.png](/img/bVbRYyx)

image.png
另外,如何在着色前清除上次的着色


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
246 views
Welcome To Ask or Share your Answers For Others

1 Answer

等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to 16892 Developer Community-Open, Learning and Share
...