Meaning of different characters.
Character | Meaning |
c | General character |
. | Match any single character |
^ | Match start of a string |
$ | Match end of a string |
* |
Match zero or many occurrences of a character |
/*match :Test the regexp in text*/
int match(char* regexp,char* text)
{
if(regexp[0] == '^')
return matchhere(regexp+1,text);
do{ /*Check the string even if it is an empty string*/
if(matchhere(regexp,text))
return 1;
}while(*text++ !='\0');
return 0;
}
/*matchhere :Test regexp at the 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 :Match zero or many occurrences of character c in text*/
int matchstar(int c,char* regexp,char* text)
{
do{
if(matchhere(regexp,text))
return 1;
}while(*text != '\0' && (*text++ == c || c == '.'));
return 0;
}
Source : http://blog.csdn.net/denieljean/article/details/6611661