-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegex.cpp
More file actions
87 lines (81 loc) · 1.67 KB
/
Regex.cpp
File metadata and controls
87 lines (81 loc) · 1.67 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
#include "Automata.cpp"
#include "sint_tree.cpp"
#include "sint_tree.h"
#include "Automata.h"
namespace Regex
{
class Re {
private:
string reg;
DFA autom;
bool compiled;
void compile(string r) {
if (!compiled)
{
autom = DFA(NFA(Sint_tree(r)));
compiled = true;
}
}
public:
Re(string r) : reg(r), compiled(false) {
compile(r);
}
Re(DFA a) : autom(a), compiled(true) {}
~Re() {}
vector<string> findall(string str) {
if (!compiled) compile(reg);
vector<string> substrings;
int i = 0, j, k;
while (i < str.length())
{
j = i;
while (j < str.length())
{
k = i;
string substr = "";
while (k <= j)
{
substr += str[k];
++k;
}
if (autom.match(substr)) {
substrings.push_back(substr);
for (k = i; k < j + 1; k++) str.erase(k);
j = i;
}
else j++;
}
++i;
}
return substrings;
}
string rec_expr() {
if (!compiled) compile(reg);
//return autom.ReFromDFA();
return reg;
}
void inverse() {
if (reg == "") reg = autom.ReFromDFA();
autom = DFA(NFA(Sint_tree(reg, true)));
}
/*DFA intesections(DFA nautom) {
if (!compiled) compile(reg);
autom.multiply(nautom).print();
}*/
bool intesections(string s, string re) {
if (!compiled) compile(reg);
DFA nautom = DFA(NFA(Sint_tree(s)));
DFA res = autom.multiply(nautom);
res.print();
return res.match(re);
}
bool match(string str) {
if (!compiled) compile(reg);
return autom.match(str);
}
void print_DFA() {
if (!compiled) compile(reg);
autom.print();
}
};
}