I’m working on writing a simple highlighter and I need to capture the all the text including the quotes, for the first word per line. How can I adjust this to do so? Currently this gets me every group of words within quotes, however i need just the first one.
Here are two regex i’ve found capture words within quotes
("[^"]*")
(".*?[^\]")
I’m just tryin to make a simple json syntax highlighter in pyside.
JavaScript
x
106
106
1
import os
2
import sys
3
from PySide2 import QtCore, QtGui, QtWidgets
4
5
class SourceEditor(QtWidgets.QPlainTextEdit):
6
def __init__(self, parent=None):
7
super(SourceEditor, self).__init__(parent)
8
font = QtGui.QFont()
9
font.setFamily('Courier')
10
font.setFixedPitch(True)
11
font.setPointSize(10)
12
self.setFont(font)
13
14
self.highlighter = Highlighter(self.document())
15
16
17
class Highlighter(QtGui.QSyntaxHighlighter):
18
def __init__(self, parent=None):
19
super(Highlighter, self).__init__(parent)
20
21
self.highlightingRules = []
22
23
singleLineCommentFormat = QtGui.QTextCharFormat()
24
singleLineCommentFormat.setFontItalic(True)
25
singleLineCommentFormat.setForeground(QtGui.QColor(115,115,115))
26
self.highlightingRules.append((QtCore.QRegExp("//[^n]*"), singleLineCommentFormat))
27
28
self.multiLineCommentFormat = QtGui.QTextCharFormat()
29
self.multiLineCommentFormat.setFontItalic(True)
30
self.multiLineCommentFormat.setForeground(QtGui.QColor(115,115,115))
31
32
quotationFormat = QtGui.QTextCharFormat()
33
quotationFormat.setForeground(QtGui.QColor(230,145,100))
34
self.highlightingRules.append((QtCore.QRegExp(""[^"]*""), quotationFormat))
35
36
self.commentStartExpression = QtCore.QRegExp("/\*")
37
self.commentEndExpression = QtCore.QRegExp("\*/")
38
39
40
def highlightBlock(self, text):
41
for pattern, format in self.highlightingRules:
42
expression = QtCore.QRegExp(pattern)
43
index = expression.indexIn(text)
44
while index >= 0:
45
length = expression.matchedLength()
46
self.setFormat(index, length, format)
47
index = expression.indexIn(text, index + length)
48
49
self.setCurrentBlockState(0)
50
51
startIndex = 0
52
if self.previousBlockState() != 1:
53
startIndex = self.commentStartExpression.indexIn(text)
54
55
while startIndex >= 0:
56
endIndex = self.commentEndExpression.indexIn(text, startIndex)
57
58
if endIndex == -1:
59
self.setCurrentBlockState(1)
60
commentLength = len(text) - startIndex
61
else:
62
commentLength = endIndex - startIndex + self.commentEndExpression.matchedLength()
63
64
self.setFormat(startIndex, commentLength, self.multiLineCommentFormat)
65
startIndex = self.commentStartExpression.indexIn(text, startIndex + commentLength);
66
67
68
if __name__ == '__main__':
69
app = QtWidgets.QApplication(sys.argv)
70
window = SourceEditor()
71
style.setStyle(widget=window)
72
window.setPlainText('''
73
[
74
{
75
"group": "Simple",
76
"name": "Simple",
77
"category name": "Apps",
78
"icon": "Simple.svg",
79
"paths": [
80
{
81
"path": "notepad.exe"
82
}
83
]
84
},
85
// some comment here
86
{
87
"group": "Simple",
88
"name": "Simple",
89
"category name": "Simple",
90
"icon": "Simple.svg"
91
"paths": [
92
{
93
"path": "notepad",
94
"args": "notepad.py"
95
},
96
{
97
"path": "run.exe",
98
}
99
]
100
}
101
]
102
''')
103
window.resize(640, 512)
104
window.show()
105
sys.exit(app.exec_())
106
Similar question…how do i capture numbers without the trailing comma?
(d+),
JavaScript
1
16
16
1
[
2
{
3
"description": null,
4
"entity": {
5
"id": 343,
6
"name": "07010",
7
"type": "Shot"
8
},
9
"id": 1673,
10
"project": {
11
"id": 9,
12
"name": "test10",
13
}
14
}
15
]
16
Advertisement
Answer
Use a capture group and return it:
JavaScript
1
2
1
^[ t]*("[^"]*")
2
See regex proof.
EXPLANATION
NODE | EXPLANATION |
---|---|
^ |
the beginning of the string |
[ t]* |
any character of: ‘ ‘, ‘t’ (tab) (0 or more times (matching the most amount possible)) |
( |
group and capture to 1: |
" |
‘”‘ |
[^"]* |
any character except: ‘”‘ (0 or more times (matching the most amount possible)) |
" |
‘”‘ |
) |
end of 1 |