728x90
반응형
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
// http://genius.cat-v.org/brian-kernighan/articles/beautiful | |
// c matches any literal character c | |
// . matches any single character | |
// ^ matches the beginning of the input string | |
// $ matches the end of the input string | |
// * matches zero or more occurrences of the previous character | |
/* match: search for regexp anywhere in text */ | |
int match(char *regexp, char *text) | |
{ | |
if (regexp[0] == '^') | |
return matchhere(regexp+1, text); | |
do { /* must look even if string is empty */ | |
if (matchhere(regexp, text)) | |
return 1; | |
} while (*text++ != '\0'); | |
return 0; | |
} | |
/* 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; | |
} |
728x90
반응형
'C C++' 카테고리의 다른 글
[StudioStyles] 비주얼 스튜디오 색 변경 (Visual Studio Color Shceme) (0) | 2011.09.18 |
---|---|
C 언어의 선대 언어 계승 기능 (0) | 2011.07.31 |
get glibc version (0) | 2011.07.16 |
리눅스 환경에서 tzset()와 fork() 간의 잠금 문제 분석 및 해결 방안 (0) | 2011.07.12 |
QtConcurrent : MapReduce 모델에 대한 Qt의 대안 (0) | 2011.01.08 |