-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathregexMatchObject.cpp
38 lines (23 loc) · 1.02 KB
/
regexMatchObject.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
#include <regex>
#include <iomanip>
#include <iostream>
#include <string>
void showCaptureGroups(const std::string& regEx, const std::string& text){
// regular expression holder
std::regex rgx(regEx);
// result holder
std::smatch smatch;
// result evaluation
if (std::regex_search(text, smatch, rgx)){
std::cout << std::setw(14) << regEx << std::setw(12) << text << std::setw(12) << smatch[0] << std::setw(10) << smatch[1] << std::setw(10) << smatch[2] << std::setw(10) << smatch[3] << std::setw(10) << smatch[4] << '\n';
}
}
int main(){
std::cout << '\n';
std::cout << std::setw(14) << "reg Expr" << std::setw(12) << "text" << std::setw(12) << "smatch[0]" << std::setw(10) << "smatch[1]" << std::setw(10) << "smatch[2]" << std::setw(10) << "smatch[3]" << std::setw(10) << "smatch[4]" << '\n';
showCaptureGroups("abc+", "abccccc");
showCaptureGroups("(a+)(b+)(c+)", "aaabccc");
showCaptureGroups("((a+)(b+)(c+))", "aaabccc");
showCaptureGroups("(ab)(abc)+", "ababcabc");
std::cout << '\n';
}