Я не эксперт по регулярным выражениям, но использую C++ answer
для обнаружения текстов между двойными кавычками, заменяя их на одинарные. Я вижу, что это работает:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class SyntaxHighlighter(QtGui.QSyntaxHighlighter):
def __init__(self, parent=None):
super(SyntaxHighlighter, self).__init__(parent)
keywordFormat = QtGui.QTextCharFormat()
keywordFormat.setForeground(QtCore.Qt.darkBlue)
keywordFormat.setFontWeight(QtGui.QFont.Bold)
keywordPatterns = ["'([^'']*)'"]
self.highlightingRules = [(QtCore.QRegExp(pattern), keywordFormat)
for pattern in keywordPatterns]
def highlightBlock(self, text):
for pattern, _format in self.highlightingRules:
expression = QtCore.QRegExp(pattern)
index = expression.indexIn(text)
while index >= 0:
length = expression.matchedLength()
self.setFormat(index, length, _format)
index = expression.indexIn(text, index + length)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
editor = QtWidgets.QTextEdit()
editor.append("string1 = 'test' and string2 = 'ajsijd'")
highlighter = SyntaxHighlighter(editor.document())
editor.show()
sys.exit(app.exec_())
