-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregex_tester.cpp
More file actions
107 lines (89 loc) · 2.77 KB
/
regex_tester.cpp
File metadata and controls
107 lines (89 loc) · 2.77 KB
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
#include "regex_tester.h"
#include <fmt/format.h>
namespace perry {
RegexTester RegexTester::instance;
bool RegexTester::validatePattern(std::string& errorMsg)
{
if (pattern.empty()) {
errorMsg = "正则表达式不能为空";
return false;
}
try {
std::regex re(pattern, flags);
errorMsg = "";
return true;
} catch (const std::regex_error& e) {
errorMsg = fmt::format("正则表达式错误: {}", e.what());
return false;
}
}
std::vector<RegexMatchResult> RegexTester::match()
{
results.clear();
if (pattern.empty() || testText.empty()) {
return results;
}
try {
std::regex re(pattern, flags);
std::sregex_iterator it(testText.begin(), testText.end(), re);
std::sregex_iterator end;
while (it != end) {
std::smatch match = *it;
RegexMatchResult result;
result.match = match.str();
result.position = static_cast<int>(match.position());
result.length = static_cast<int>(match.length());
results.push_back(result);
++it;
}
} catch (const std::regex_error&) {
}
return results;
}
std::string RegexTester::getResultString()
{
if (results.empty()) {
return "无匹配结果";
}
std::string output;
for (size_t i = 0; i < results.size(); ++i) {
const auto& r = results[i];
output += fmt::format("[{}] \"{}\"\n", i + 1, r.match);
output += fmt::format(" 位置: {}, 长度: {}\n", r.position, r.length);
}
return output;
}
std::string RegexTester::replace(const std::string& replacement)
{
if (pattern.empty() || testText.empty()) {
return testText;
}
try {
std::regex re(pattern, flags);
return std::regex_replace(testText, re, replacement);
} catch (const std::regex_error&) {
return testText;
}
}
std::string RegexTester::getPresetPattern(int index)
{
static const std::vector<std::string> presets = {
"",
R"(-?\d+)",
R"(-?\d+\.?\d*)",
R"(([0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F]{2})",
R"((25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?))",
R"(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|::|(:[0-9a-fA-F]{1,4}){1,7}|[0-9a-fA-F]{1,4}::([0-9a-fA-F]{1,4}:){0,5}[0-9a-fA-F]{1,4})",
R"([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})",
R"(1[3-9]\d{9})",
R"(\d{17}[\dXx])",
R"(https?://[^\s]+)",
R"(\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))",
R"(([01]?\d|2[0-3]):[0-5]\d:[0-5]\d)",
};
if (index < 0 || index >= static_cast<int>(presets.size())) {
return "";
}
return presets[index];
}
}