-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregex_samples.cpp
More file actions
152 lines (120 loc) · 3.17 KB
/
regex_samples.cpp
File metadata and controls
152 lines (120 loc) · 3.17 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <iostream>
#include <regex>
#include <string>
#include <iterator>
#include <algorithm>
/*
* \t: Tab
* \d: digit [[:digit:]]
* \v: vertical Tab
* .: anything
* \r: return
* \D: not digit [^[:digit]]
* \s: Space [[:space:]]
* \S: not Space [^[:space]]
* \w: word [_[:alnum:]]
* \W: not word [^_[:alnum:]]
* \u: unicode
* \x: hexacode
* [xyz]: x or y or z
* [^xyz]: not ( x or y or z)
* classes:
* [:alnum:] : alpha numerical
* [:alpha:] : alpha
* [:blank;] : blank
* [:cntrl:] : control
* [:digit:] : digit
* [:lower:] : lower
* [:upper:] : upper
* [:xdigit:]: hexadecimal
* [:punct:] : punctions
* [:d:] : deciaml
* [:w:] : word
* [:s:] : whitespace
*
* *: 0 or more
* +: 1 or more
* ?: 0 or 1
* {int}: int
* {int,}: int or more
* {min,max}: min = max
*
* ((subpattern)): groups
* (?:subpattern): passive group => doesnt create back reference
* ^: beginning of line
* $: end of the line
* \b: word boundary
* \B: not word boundary
* | : seperater...two alternative patterns
*/
// regex_token_iterator
void token_iterator_test(){
std::cout << "token_iterator:" << std::endl;
const std::string text = "sample text to split words";
std::regex re(R"(\s+)");
// get the left and right of searched items
std::sregex_token_iterator beg(text.begin(), text.end(), re, -1);
std::sregex_token_iterator end;
while( beg != end) {
std::cout << *beg++ << '\n';
}
std::cout << std::endl;
std::sregex_token_iterator beg2(text.begin(), text.end(), re);
std::sregex_token_iterator end2;
std::cout << "token_iterator2: " << std::endl;
while( beg2 != end2) {
std::cout << *beg2++ << '\n';
}
std::cout << std::endl;
std::sregex_token_iterator beg3(text.begin(), text.end(), re, 1);
std::sregex_token_iterator end3;
std::cout << "token_iterator3" << std::endl;
while ( beg3 != end3 ){
std::cout << *beg3++ << '\n';
}
std::cout << std::endl;
}
void regex_match_test() {
std::regex re("(soft)(.*)");
std::string word = "software";
auto result = std::regex_match(word, re);
std::cout << std::boolalpha << result << std::endl;
std::smatch sm;
std::regex_match(word, sm, re);
std::cout << sm.size() << std::endl;
std::cout << "word:" << sm[0] << '\n'
<< "first:" << sm[1] << '\n'
<< "second:" << sm[2] << std::endl;
}
void regex_search_test() {
std::string text = "hello world";
std::regex re("(wor)(.*)");
std::smatch sm;
std::regex_search(text, sm, re);
std::cout << sm[0] << ":" << sm[1] << ";" << sm[2] << std::endl;
std::string s = "this subject has a submarine as a subsequence";
std::smatch m;
std::regex re2("\\b(sub)([^ ]*)");
while (std::regex_search(s, m, re2)) {
for (auto x : m) {
std::cout << x << std::endl;
}
s = m.suffix().str();
std::cout << s << std::endl;
}
}
void split() {
const std::string text = "a b c d e f g h i j k l m o";
std::regex re("\\s+");
std::sregex_token_iterator beg(text.begin(), text.end(), re, -1);
std::sregex_token_iterator end;
while (beg != end) {
std::cout << *beg++ << " ";
}
}
int main() {
token_iterator_test();
regex_match_test();
regex_search_test();
split();
}