Debian Codegraph @

yo/stl/examples/regex.cc

raw
1 #include <string>
2 #include <iostream>
3 #include <regex>
4
5 using namespace std;
6
7
8 int main()
9 {
10 cout << "Empty pattern to quit, empty target to specify new pattern\n";
11
12 while (true)
13 {
14 string pat;
15
16 cout << "Pattern: ";
17 getline(cin, pat);
18 if (pat.empty())
19 break;
20
21 while (true)
22 {
23 cout << "Target: ";
24 string target;
25 getline(cin, target);
26 if (target.empty())
27 break;
28
29 regex re(pat);
30 smatch sm;
31
32 cout << "Before the match: empty = " << sm.empty() << '\n';
33
34 regex_search(target, sm, re);
35 cout << sm.size() << '\n';
36
37 for (size_t idx = 0; idx != sm.size(); ++idx)
38 cout << sm[idx] << '\n';
39
40 cout << "After the match: empty = " << sm.empty() << '\n';
41
42 for (auto &m: sm)
43 cout << "sub match: " << m << '\n';
44
45 while (true)
46 {
47 cout << "Which match position (empty for none)? ";
48 string str;
49 getline(cin, str);
50 if (str.empty())
51 break;
52 cout << "nr " << str << ": " << sm.str(stoul(str)) <<
53 '\n';
54 }
55 }
56 }
57 }