-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregexp.cpp
More file actions
47 lines (42 loc) · 1004 Bytes
/
regexp.cpp
File metadata and controls
47 lines (42 loc) · 1004 Bytes
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
#include "regexp.h"
regexp::regexp()
{
//ctor
}
regexp::~regexp()
{
//dtor
}
// match: search regexp in text
int regexp::match(char *regexp, char *text)
{
if (regexp[0] == '^')
return matchhere(regexp+1, text);
do {
if (matchhere(regexp, text))
return 1;
} while (*text++ != '\0');
return 0;
}
// matchhere: search regexp in the begining of text
int regexp::matchhere(char *regexp, char *text)
{
if (regexp[0] == '\0')
return 1;
if (regexp[1] == '*')
return matchstar(regexp[0], regexp+2, text);
if (regexp[0] == '$' && regexp[1] == '\0')
return *text == '\0';
if (*text != '\0' && (regexp[0] == '.' || regexp[0] == *text))
return matchhere(regexp+1, text+1);
return 0;
}
// matchstar: search c*regexp in the beginning of text
int regexp::matchstar(int c, char *regexp, char *text)
{
do {
if (matchhere(regexp, text))
return 1;
} while (*text != '\0' && (*text++ == c || c == '.'));
return 0;
}