-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregexp.c
86 lines (61 loc) · 1.96 KB
/
regexp.c
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
/* regexp.c
a simple regular expression check
matches: c literal match with single character c
. match any single character
^ match start of line
$ match end of line
* match zero or more occurrences of previous character
extracted from "Beautiful Code", Oram & Wilson, Chapter 1
*/
#include <stdio.h>
#define DEBUG 0
int matchhere (char *regexp, char *text);
int matchstar (char c, char *regexp, char *text);
// match: search for regexp anywhere in text
int match (char *regexp, char *text) {
#if DEBUG
printf ("match %s %s$\n", regexp, text);
#endif
// match start of line
if (regexp[0] == '^')
return matchhere (regexp+1, text);
do { // check even if string is empty
if (matchhere (regexp, text))
return 1;
} while (*text++ != '\0');
return 0;
}
// matchhere: search for regexp at the beginning of text
int matchhere (char *regexp, char *text) {
#if DEBUG
printf ("matchhere %s %s$\n", regexp, text);
#endif
// end of regexp
if (regexp[0] == '\0') return 1;
// repeated character
if (regexp[1] == '*')
return matchstar(regexp[0], regexp+2, text);
// match end of line
if (regexp[0] == '$' && regexp[1] == '\0')
return *text == '\0';
// match characters
if (*text != '\0' &&
(regexp[0] == '.' || regexp[0] == *text))
return matchhere (regexp+1, text+1);
return 0;
}
// matchstar: leftmost longest search for c*regexp
int matchstar (char c, char *regexp, char *text) {
#if DEBUG
printf ("matchstar %c %s %s$\n", c, regexp, text);
#endif
char *t;
// match repeated character
for (t = text; *t != '\0' && (*t == c || c == '.'); t++);
// resume matching regexp after repeated character
do {
if (matchhere (regexp, t))
return 1;
} while (t-- > text);
return 0;
}