├── Makefile ├── README ├── simple_re_match.c └── simple_re_match.h /Makefile: -------------------------------------------------------------------------------- 1 | all: simple_re_match 2 | simple_re_match: 3 | gcc simple_re_match.c -o simple_re_match 4 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | A simple regular expression matcher based on Rob Pike's implementation. 2 | -------------------------------------------------------------------------------- /simple_re_match.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "simple_re_match.h" 4 | 5 | /* 6 | Regular expresions: 7 | c matches any literal character c 8 | . matches any single character 9 | ^ matches the beginning of the input string 10 | $ matches the end of the input string 11 | * matches zero or more occurrences of the previous character 12 | + matches one or more occurrences of the previous character 13 | */ 14 | 15 | /* 16 | @brief search for regexp anywhere in the text and return the index that matches 17 | @param regular expression string 18 | @param string to perform the regex on 19 | */ 20 | int 21 | match(char *regexp, char *text) 22 | { 23 | if (regexp[0] == '^') 24 | return matchhere(regexp+1, text); 25 | do { //should check if the string is empty 26 | if (matchhere(regexp, text)) 27 | return 1; 28 | }while(*text++ != '\0'); 29 | return 0; 30 | } 31 | 32 | int 33 | matchhere(char *regexp, char *text) 34 | { 35 | if (regexp[0] == '\0') 36 | return 1; 37 | if (regexp[1] == '*') 38 | return matchstar(regexp[0], regexp+2, text); 39 | if (regexp[1] == '+') 40 | return matchplus(regexp[0], regexp+2, text); 41 | if (regexp[0] == '\0' && regexp[1] == '\0') 42 | return *text == '\0'; 43 | if (*text != '\0' && (regexp[0] == '.' || regexp[0] == *text)) 44 | return matchhere(regexp + 1, text + 1); 45 | return 0; 46 | } 47 | 48 | /* matchstar: leftmost longest search for c*regexp */ 49 | int 50 | matchstar(int c, char *regexp, char *text) 51 | { 52 | char *t; 53 | for (t = text; *t != '\0' && (*t == c || c == '.'); t++) 54 | ; 55 | do { /* * matches zero or more */ 56 | if (matchhere(regexp, t)) 57 | return 1; 58 | } while (t-- > text); 59 | return 0; 60 | } 61 | 62 | int 63 | matchplus(int c, char *regexp, char *text) 64 | { 65 | int i = 0; 66 | do { //a matches one or more instances 67 | if (matchhere(regexp, text)) 68 | if (i == 0) 69 | i++; 70 | else 71 | return 1; 72 | } while (*text != '\0' && (*text++ == c || c == '.')); 73 | return 0; 74 | } 75 | 76 | int main(){ 77 | char *regex = "c+"; 78 | char *text = "caglar"; 79 | if(match(regex, text)) 80 | printf("Ok!\n"); 81 | else 82 | printf("No!\n"); 83 | return 0; 84 | } 85 | -------------------------------------------------------------------------------- /simple_re_match.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: simple_re_match.h 5 | * 6 | * Description: 7 | * 8 | * Version: 1.0 9 | * Created: 10/06/2011 01:19:46 PM 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: YOUR NAME (), 14 | * Company: 15 | * 16 | * ===================================================================================== 17 | */ 18 | #ifndef SIMPLE_RE_MATCH 19 | #define SIMPLE_RE_MATCH 20 | 21 | int 22 | match(char *regexp, char *text); 23 | 24 | int 25 | matchhere(char *regexp, char *text); 26 | 27 | int 28 | matchstar(int c, char *regexp, char *text); 29 | 30 | int 31 | matchplus(int c, char *regexp, char *text); 32 | 33 | #endif 34 | --------------------------------------------------------------------------------