A few days ago when using grep, found that grep's * incredibly support problems, and can not get the desired results, so took some time to implement the regular expression x match, the following function can match *,?。
The code is short, but it's efficient.
BOOL Match_star (const char* text,const char* pattern) {const char *CP = text; Const char* PP = pattern; const char *PS1, *PS2; if (!*pattern) return true; while (*CP) {PS1 = CP; PS2 = PP; while (*ps1 && *ps2) {if (*PS2 = = ' * ') { CP = Ps1-1; pp = ps2+1; Break } else if (0 = = (*PS1-*PS2) | | *ps2 = = '? ') {ps1++; ps2++; } else {break; }} if (!*PS2) {return true; } cp++; } return false;}
Test passes on Windows VC2010 and Linux g++.
When the match X is implemented, a technique is used to compare two new strings and discard what has already been compared.
For example, the string is 1234567890, the pattern string is 1*23*0 and 1? 3 can match the success.
If there is a problem, please correct me, thank you!
Implementation of the regular expression * and? The