Debian Codegraph @

yo/stl/examples/regexsearch.cc

raw
1 #include <iostream>
2 #include <string>
3 #include <regex>
4
5 using namespace std;
6
7 int main()
8 {
9 while (true)
10 {
11 cout << "Enter a pattern or plain Enter to stop: ";
12
13 string pattern;
14 if (not getline(cin, pattern) or pattern.empty())
15 break;
16
17 regex re(pattern);
18 while (true)
19 {
20 cout << "Enter a target text for `" << pattern << "'\n"
21 "(plain Enter for the next pattern): ";
22
23 string text;
24 if (not getline(cin, text) or text.empty())
25 break;
26
27 smatch results;
28 if (not regex_search(text, results, re))
29 cout << "No match\n";
30 else
31 {
32 cout << "Prefix: " << results.prefix() << "\n"
33 "Match: " << results.str() << "\n"
34 "Suffix: " << results.suffix() << "\n";
35 for (size_t idx = 1; idx != results.size(); ++idx)
36 cout << "Match " << idx << " at offset " <<
37 results.position(idx) << ": " <<
38 results.str(idx) << '\n';
39 }
40 }
41 }
42 }