|
10 | 10 | #include <iostream> |
11 | 11 | #include <string> |
12 | 12 | #include <regex> |
13 | | - |
| 13 | +using namespace std; |
14 | 14 | int main() { |
15 | | - std::string fnames[] = {"foo.txt", "bar.txt", "test", "a0.txt", "AAA.txt"}; |
| 15 | + string fnames[] = {"foo.txt", "bar.txt", "test", "a0.txt", "AAA.txt"}; |
16 | 16 | // In C++, `\` will be used as an escape character in the string. |
17 | 17 | // In order for `\.` to be passed as a regular expression, |
18 | 18 | // it is necessary to perform second escaping of `\`, thus we have `\\.` |
19 | | - std::regex txt_regex("[a-z]+\\.txt"); |
| 19 | + regex txt_regex("[a-z]+\\.txt"); |
20 | 20 | for (const auto &fname: fnames) |
21 | | - std::cout << fname << ": " << std::regex_match(fname, txt_regex) << std::endl; |
| 21 | + cout << fname << ": " << regex_match(fname, txt_regex) << endl; |
22 | 22 |
|
23 | | - std::regex base_regex("([a-z]+)\\.txt"); |
24 | | - std::smatch base_match; |
| 23 | + regex base_regex("([a-z]+)\\.txt"); |
| 24 | + smatch base_match; |
25 | 25 | for(const auto &fname: fnames) { |
26 | | - if (std::regex_match(fname, base_match, base_regex)) { |
| 26 | + if (regex_match(fname, base_match, base_regex)) { |
27 | 27 | // the first element of std::smatch matches the entire string |
28 | 28 | // the second element of std::smatch matches the first expression with brackets |
29 | 29 | if (base_match.size() == 2) { |
30 | | - std::string base = base_match[1].str(); |
31 | | - std::cout << "sub-match[0]: " << base_match[0].str() << std::endl; |
32 | | - std::cout << fname << " sub-match[1]: " << base << std::endl; |
| 30 | + string base = base_match[1].str(); |
| 31 | + cout << "sub-match[0]: " << base_match[0].str() << endl; |
| 32 | + cout << fname << " sub-match[1]: " << base <<endl; |
33 | 33 | } |
34 | 34 | } |
35 | 35 | } |
|
0 commit comments