Skip to content

Commit e56cc54

Browse files
authored
Update 6.1.regex.cpp
1 parent 212f4f3 commit e56cc54

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

code/6/6.1.regex.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,26 @@
1010
#include <iostream>
1111
#include <string>
1212
#include <regex>
13-
13+
using namespace std;
1414
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"};
1616
// In C++, `\` will be used as an escape character in the string.
1717
// In order for `\.` to be passed as a regular expression,
1818
// 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");
2020
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;
2222

23-
std::regex base_regex("([a-z]+)\\.txt");
24-
std::smatch base_match;
23+
regex base_regex("([a-z]+)\\.txt");
24+
smatch base_match;
2525
for(const auto &fname: fnames) {
26-
if (std::regex_match(fname, base_match, base_regex)) {
26+
if (regex_match(fname, base_match, base_regex)) {
2727
// the first element of std::smatch matches the entire string
2828
// the second element of std::smatch matches the first expression with brackets
2929
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;
3333
}
3434
}
3535
}

0 commit comments

Comments
 (0)