-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathLearnRegexPyQt5.py
134 lines (107 loc) · 5.13 KB
/
LearnRegexPyQt5.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# Python Regular Expressions - GUI Interactive Tool
# Chandra Lingam
# http://c2designs.cloud
#
# Compatible with 3.x version of python
# Distributed under GNU Public License - https://www.gnu.org/licenses/gpl-3.0.en.html
# Version History
# Original Version - Chandra Lingam
# PyQt5 migration - Sash Eranki
import sys
from PyQt5 import QtGui, QtWidgets #modified from PyQt4 to PyQt5 for anaconda 3.5 distr
import re
import time
class LearnRegex(QtWidgets.QWidget):
def __init__(self):
super(LearnRegex, self).__init__()
self.init_ui()
def init_ui(self):
self.setFont(QtGui.QFont("Sans Serif", 11))
self.txtPattern = QtWidgets.QLineEdit()
self.txtRepPattern = QtWidgets.QLineEdit()
# Changed to plain text
self.txtText= QtWidgets.QPlainTextEdit()
self.txtResult = QtWidgets.QTextEdit()
self.regex = None
self.regexText = None
lblPattern = QtWidgets.QLabel('Pattern')
lblRepPattern = QtWidgets.QLabel('Replacement Pattern')
lblText = QtWidgets.QLabel('Text')
lblResult = QtWidgets.QLabel('Result')
btnMatch = QtWidgets.QPushButton('Match')
btnNextMatch = QtWidgets.QPushButton('Next Match')
btnReplace = QtWidgets.QPushButton('Replace')
btnSplit = QtWidgets.QPushButton('Split')
btnMatch.clicked.connect(self.match_click)
btnNextMatch.clicked.connect(self.next_match_click)
btnReplace.clicked.connect(self.replace_click)
btnSplit.clicked.connect(self.split_click)
grid = QtWidgets.QGridLayout()
grid.setSpacing(10)
grid.addWidget(lblPattern, 1, 0)
grid.addWidget(self.txtPattern, 1, 1, 1, 4)
grid.addWidget(lblRepPattern, 2, 0)
grid.addWidget(self.txtRepPattern, 2, 1, 1, 4)
grid.addWidget(lblText, 3, 0)
grid.addWidget(self.txtText, 3, 1, 1, 4)
grid.addWidget(btnMatch, 4, 1)
grid.addWidget(btnNextMatch, 4, 2)
grid.addWidget(btnReplace, 4, 3)
grid.addWidget(btnSplit, 4, 4)
grid.addWidget(lblResult, 5, 0)
grid.addWidget(self.txtResult, 5, 1, 5, 4)
self.setLayout(grid)
self.setGeometry(300, 300, 860, 620)
self.setWindowTitle('Learning Regex with Python')
self.show()
def match_click(self):
self.txtResult.setText ('Value,Index,Length,ElapsedTime(s)')
try:
self.regex = re.compile(self.txtPattern.text())
except Exception as e:
QtWidgets.QMessageBox.critical(self, 'Error', 'Pattern error: {0}'.format(str(e)))
return
self.regex_text = self.txtText.toPlainText()
self.regex_group_index = dict((v,k) for k,v in self.regex.groupindex.items())
self.match_iter = self.regex.finditer(self.regex_text)
self.highlight_match()
def next_match_click(self):
self.highlight_match()
def replace_click(self):
self.txtResult.setText(re.sub(self.txtPattern.text(), self.txtRepPattern.text(), self.txtText.toPlainText()))
def split_click(self):
self.txtResult.setText("")
for s in re.split(self.txtPattern.text(), self.txtText.toPlainText()):
self.txtResult.append(s)
def highlight_match(self):
start_time = None
try:
#self.txtText.setAcceptRichText(False)
self.txtText.setPlainText(self.regex_text)
start_time = time.time()
match = self.match_iter.__next__()
end_time = time.time()
self.txtResult.append ('{0},{1},{2},{3:.2f}'.format(match.group(0),match.start(), match.end()-match.start(), end_time - start_time))
for i in range(0, len(match.groups())):
gn = i+1
group_name = None
if gn in self.regex_group_index:
group_name = self.regex_group_index[gn]
self.txtResult.append (' Group:{0}, Name:{1}, Value: {2}'.format(gn, group_name, match.group(gn)))
self.highlighter(match.start(), match.end())
except StopIteration:
end_time = time.time()
self.txtResult.append ('End, Elapsed Time (s): {0:0.2f}'.format(end_time - start_time))
def highlighter(self,start,end):
format = QtGui.QTextCharFormat()
format.setBackground(QtGui.QBrush(QtGui.QColor("yellow")))
cursor = self.txtText.textCursor()
cursor.setPosition(start)
cursor.movePosition(QtGui.QTextCursor.Right,QtGui.QTextCursor.KeepAnchor,end-start)
cursor.mergeCharFormat(format)
def main():
app = QtWidgets.QApplication(sys.argv)
ex = LearnRegex()
sys.exit(app.exec_())
if __name__ == '__main__':
main()