Created
September 24, 2014 15:29
-
-
Save ericpony/eb40edd5ee0ae6d96b53 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* matchhere: search for regexp at beginning of text */ | |
int 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 for c*regexp at beginning of text */ | |
int matchstar(int c, char *regexp, char *text) | |
{ | |
do { /* a * matches zero or more instances */ | |
if (matchhere(regexp, text)) | |
return 1; | |
} while (*text != '\0' && (*text++ == c || c == '.')); | |
return 0; | |
} | |
/* match: search for regexp anywhere in text */ | |
int match(char *regexp, char *text) | |
{ | |
if (regexp[0] == '^') | |
return matchhere(regexp+1, text); | |
/* must look even if string is empty */ | |
do { | |
if (matchhere(regexp, text)) | |
return 1; | |
} while (*text++ != '\0'); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment