-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregex.cpp
122 lines (105 loc) · 3.52 KB
/
regex.cpp
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
#include "regex.h"
#include "ui_regex.h"
#include "boost/regex.hpp"
#include <QString>
#include <QMessageBox>
#include <QTextCursor>
#include <QTextCharFormat>
RegEx::RegEx(QWidget *parent) :
QDialog(parent),
ui(new Ui::RegEx)
{
ui->setupUi(this);
setFixedSize(this->width(),this->height());
}
RegEx::~RegEx()
{
delete ui;
}
void RegEx::setMatchHighlighting(std::map<size_t, size_t> match_pos) {
QTextCursor cursor = ui->textEdit_Text->textCursor();
std::map<size_t, size_t>::const_iterator itr;
for (itr = match_pos.begin(); itr != match_pos.end(); ++itr) {
QTextCharFormat fmt;
fmt.setBackground(QBrush(Qt::cyan));
QTextCursor helper = cursor;
helper.setPosition(itr->first, QTextCursor::MoveAnchor);
helper.setPosition(itr->second, QTextCursor::KeepAnchor);
helper.setCharFormat(fmt);
}
}
void RegEx::on_pushButton_Match_clicked() {
ui->textEdit_Text->setPlainText(ui->textEdit_Text->toPlainText());
QString text, sReg;
text = ui->textEdit_Text->toPlainText();
if (text.isEmpty()) {
ui->textEdit_Text->setFocus();
return;
}
sReg = ui->lineEdit_RegEx->text();
if (sReg.isEmpty()) {
ui->lineEdit_RegEx->setFocus();
return;
}
boost::regex::flag_type flag = boost::regex_constants::normal;
if (ui->checkBox_ICase->isChecked()) flag |= boost::regex::icase;
try {
boost::wregex reg(sReg.toStdWString(), flag);
QString result;
std::wstring ws_text(text.toStdWString());
boost::wsmatch match;
std::map<size_t, size_t> match_pos;
size_t n = 1;
size_t pos_base = 0;
while (boost::regex_search(ws_text, match, reg)) {
size_t real_pos = match.position() + pos_base;
match_pos.insert(
std::pair<size_t, size_t>(real_pos, real_pos + match.length()));
result += "match(" + QString::number(n++) + "): pos=";
result += QString::number(real_pos) + LINE_BREAK;
for (size_t i = 0; i < match.size(); ++i) {
result += " sub(" + QString::number(i) +
"):" + QString::fromStdWString(match[i].str());
result += LINE_BREAK;
}
pos_base += ws_text.size() - match.suffix().str().size();
ws_text = match.suffix().str();
result += LINE_BREAK;
}
result = "total(" + QString::number(--n) + ") match:\n" + result;
ui->textEdit_Match_Result->setPlainText(result);
setMatchHighlighting(match_pos);
} catch (boost::regex_error& e) {
QMessageBox::warning(NULL, "error", e.what());
}
}
void RegEx::on_pushButton_Replace_clicked() {
QString text, sReg, sReplace;
text = ui->textEdit_Text->toPlainText();
if (text.isEmpty()) {
ui->textEdit_Text->setFocus();
return;
}
sReg = ui->lineEdit_RegEx->text();
if (sReg.isEmpty()) {
ui->lineEdit_RegEx->setFocus();
return;
}
sReplace = ui->lineEdit_Replace->text();
if (sReplace.isEmpty()) {
ui->lineEdit_Replace->setFocus();
return;
}
boost::regex::flag_type flag = boost::regex_constants::normal;
if (ui->checkBox_ICase->isChecked()) flag |= boost::regex::icase;
try {
boost::wregex reg(sReg.toStdWString(), flag);
std::wstring ws_text(text.toStdWString());
std::wstring replace_result =
boost::regex_replace(ws_text, reg, sReplace.toStdWString());
ui->textEdit_Replace_Result->setPlainText(
QString::fromStdWString(replace_result));
} catch (boost::regex_error& e) {
QMessageBox::warning(NULL, "error", e.what());
}
}