-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathRegExpr.cpp
More file actions
224 lines (181 loc) · 6.71 KB
/
RegExpr.cpp
File metadata and controls
224 lines (181 loc) · 6.71 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// =====================================================================================
// Regular Expressions
// =====================================================================================
module modern_cpp:regexpr;
namespace RegularExpressions {
static void test_01_simple_regex()
{
// simple example
std::regex expression{ "[a-z]+\\.txt" };
std::string names[] = {
"foo.txt",
"bar.txt",
"baz.dat",
"anyfile"
};
for (const auto& name : names) {
bool result{ std::regex_match(name, expression) };
std::cout << std::boolalpha << name << ": " << result << std::endl;
}
}
static void test_02_simple_regex()
{
// simple example - counting the number of all matches
std::regex expression{ "[^\\s]+" };
std::string text = { "std::regex_iterator is a read-only iterator" };
auto iterator = std::sregex_iterator(
text.begin(),
text.end(),
expression
);
while (iterator != std::sregex_iterator{}) {
const auto& token = *iterator;
std::println("{}", token.str());
++iterator;
}
}
static void test_02_capturing_groups()
{
// example from 'geeksforgeeks'
std::string sp{ "geeksforgeeks" };
std::regex re{ "(geeks)(.*)" };
// flag type for determining the matching behavior
// && here it is for matches on strings.
std::smatch match;
// we can use member function on match
// to extract the matched pattern.
if (std::regex_search(sp, match, re) == true) {
// The size() member function indicates the
// number of capturing groups plus one for the overall match
// match size = Number of capturing group + 1
// (.*) which "forgeeks" ).
std::cout << "Match size = " << match.size() << std::endl;
// Capturing group is index from 0 to match_size -1
// .....here 0 to 2
// pattern at index 0 is the overall match "geeksforgeeks"
// pattern at index 1 is the first capturing group "geeks"
// pattern at index 2 is the 2nd capturing group "forgeeks"
std::cout << "Whole match : " << match.str(0) << std::endl;
std::cout << "First capturing group is '" << match.str(1)
<< "' which is captured at index " << match.position(1)
<< std::endl;
std::cout << "Second capturing group is '" << match.str(2)
<< "' which is captured at index " << match.position(2)
<< std::endl;
}
else {
std::cout << "No match is found" << std::endl;
}
}
static void test_03_capturing_group_vs_non_capturing_group_01()
{
// example: capturing groups
std::regex re{ "(https?|s?ftp)://([^/\r\n]+)(/[^\r\n]*)?" };
std::string paths[] = {
"http://stackoverflow.com/",
"https://stackoverflow.com/questions/tagged/regex",
"sftp://home/remote_username/filename.zip",
"ftp://home/ftpuser/remote_test_dir",
};
for (const auto& path : paths) {
std::smatch sm;
bool success{ std::regex_match(path, sm, re) };
if (success) {
std::string protocol{ sm[1] };
std::string domain{ sm[2] };
std::string dir{ sm[3] };
std::cout
<< "Valid URL: " << path << " ==> "
<< protocol << "-" << domain << "-" << dir << std::endl;
}
else {
std::cout << "Invalid URL: " << path << std::endl;
}
}
}
static void test_04_capturing_group_vs_non_capturing_group_02()
{
// example: non-capturing group
std::regex re("(?:https?|s?ftp)://([^/\r\n]+)(/[^\r\n]*)?");
std::string paths[] = {
"http://stackoverflow.com/",
"https://stackoverflow.com/questions/tagged/regex",
"sftp://home/remote_username/filename.zip",
"ftp://home/ftpuser/remote_test_dir",
};
for (const auto& path : paths) {
std::smatch sm;
bool success{ std::regex_match(path, sm, re) };
if (success) {
std::string domain = sm[1];
std::string dir = sm[2];
std::cout <<
"Valid URL: " << path << " ==> "
<< domain << " - " << dir << std::endl;
}
else {
std::cout << "Invalid URL: " << path << std::endl;
}
}
}
static void test_05_datum_01()
{
// example: date, non-capturing groups
std::regex re{ "\\d{4}/(?:0?[1-9]|1[0-2])/(?:0?[1-9]|[1-2][0-9]|3[0-1])" };
std::string dates[] = {
"2000/06/15",
"200/6/15",
"2020/0/32",
"0001/1/1"
};
for (const auto& date : dates) {
bool success{ std::regex_match(date, re) };
if (success) {
std::cout << "Valid date: " << date << std::endl;
}
else {
std::cout << "Invalid date: " << date << std::endl;
}
}
}
static void test_06_datum_02()
{
// example: date, capturing groups
std::regex re{ "(\\d{4})/(0?[1-9]|1[0-2])/(0?[1-9]|[1-2][0-9]|3[0-1])" };
std::string dates[] = {
"2000/06/15",
"200/6/15",
"2020/0/32",
"0001/1/1"
};
for (const auto& date : dates) {
std::smatch sm;
bool success{ std::regex_match(date, sm, re) };
if (success) {
int year{ stoi(sm[1]) };
int month{ stoi(sm[2]) };
int day{ stoi(sm[3]) };
std::cout
<< "Valid date: " << date << " ==> " << year
<< "-" << month << "-" << day << std::endl;
}
else {
std::cout << "Invalid date: " << date << std::endl;
}
}
}
}
void main_regular_expressions()
{
using namespace RegularExpressions;
test_01_simple_regex();
test_02_simple_regex();
test_02_capturing_groups();
test_03_capturing_group_vs_non_capturing_group_01();
test_04_capturing_group_vs_non_capturing_group_02();
test_05_datum_01();
test_06_datum_02();
}
// =====================================================================================
// End-of-File
// =====================================================================================