Today's Question:  What are you most afraid of as a programmer?

# How regular expression works

Peter      2012-06-25 05:23:41      2,114    0    0

Rob Pike wrote 30 lines of codes to realize a simple regular expression matcher in his book The practice of Programming. This piece of code is really cool. Let's take a look at the code.

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

### 0 COMMENT

#### Client brief vs client budget

By sonic0002
Sometimes plans are quite different from realities. This is true especially in IT induestries. Usually when a client starts to present their plan, they have a really great vision. However when comes to the budget, the client runs into difficult.