-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_regex.cpp
40 lines (34 loc) · 1009 Bytes
/
test_regex.cpp
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
#include <iostream>
#include <regex>
#include <string>
using namespace std;
// accept a pattern and a set of lines from input
// check the pattern and search for lines with that pattern
int main() {
regex pattern;
string input;
cout << "enter pattern: ";
getline(cin, input); // read pattern
try {
pattern = input; // this checks input
cout << "pattern: " << input << '\n';
}
catch (regex_error) {
cout << input << " is not a valid regular expression\n";
return 1;
}
cout << "now enter lines:\n";
int lineno = 0;
for (string line; getline(cin, line);) {
++lineno;
smatch matches;
if (regex_search(line, matches, pattern)) {
cout << "line " << lineno << ": " << line << '\n';
for (int i = 0; i < matches.size(); ++i)
cout << "\tmatches[" << i << "]: " << matches[i] << '\n';
}
else
cout << "didn't match\n";
}
return 0;
}