Topic:
请实现一个函数用来匹配包含‘.’和‘*’的正则表达式。模式中的字符’.’表示任意一个字符,而‘*’表示它前面的字符可以出现任意次(含0次)。本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串“aaa”与模式“a.a”和“ab*ac*a”匹配,但与“aa.a”及“ab*a”均不匹配。
Analytical:
string str = "AAA"; Pattern string Pattern = ". B*ac*a"
Each time a character in Str and pattern is matched, if matched, the next character is matched, otherwise, a mismatch is returned.
Match recursive function matches (str, pattern).
If the next character of the pattern-matching character is ' * ':
- If the current character of the Pttern matches the current character of Str: There are three possible scenarios
- Pttern the current character can match 0 characters in str: match (str, pattern+2)
- Pttern the current character can match 1 characters in Str: match (str+1, pattern+2)
- Pttern the current character can match multiple characters in Str: match (str+1, pattern)
- If the current characters of the Pttern and Str do not match
- Pttern the current character can match 0 characters in str: (str, pattern+2)
If the next character of the pattern-matching character is not ' * ', the characters are matched verbatim.
The case for '. ' is relatively simple, '. ' and a character matching match (str+1, pattern+1)
It is also important to note that the empty string "" and ". *" are matched
BOOLMatchcore (Const Char*Str,Const Char* pattern) {if(*Str==' + '&& *pattern = =' + ')return true;//if (*str = = ' && *pattern! = ') ' Return false: not valid, such as str = "", pattern= ". *" if(*Str!=' + '&& *pattern = =' + ')return false;if(* (pattern+1) ==' * ') {if(*pattern = = *Str|| *pattern = ='. '&& *Str!=' + ') {//Three cases: * The previous character appears 0 times, appears once, appears multiple times. Pattern+2 means skipping the current character and ' * ' returnMatchcore (Str, pattern+2) || Matchcore (Str+1, pattern+2) || Matchcore (Str+1, pattern); }Else{//No match, appears 0 times (including str= "", pattern= ". *") returnMatchcore (Str, pattern+2); } }if(*Str= = *pattern | | *pattern = ='. '&& *Str!=' + ')returnMatchcore (Str+1, pattern+1);return false;}BOOLMatch (Const Char*Str,Const Char* pattern) {if(Pattern = = NULL | |Str= = NULL)return false;returnMatchcore (Str, pattern);}
Test Case:
From: Sword refers to offer source GitHub
//==================== Test Code ====================voidTest (Char* TestName,Char*string,Char* Pattern,BOOLExpected) {if(testname! = NULL)printf("%s begins:", testname);if(Match (string, pattern) = = expected)printf("passed.\n");Else printf("failed.\n");}intMainintargcChar* argv[]) {Test ("Test01","","",true); Test ("Test02","",".*",true); Test ("Test03","",".",false); Test ("Test04","","c*",true); Test ("Test05","a",".*",true); Test ("Test06","a","A.",false); Test ("Test07","a","",false); Test ("Test08","a",".",true); Test ("Test09","a","ab*",true); Test ("Test10","a","Ab*a",false); Test ("Test11","AA","AA",true); Test ("Test12","AA","A *",true); Test ("Test13","AA",".*",true); Test ("Test14","AA",".",false); Test ("Test15","AB",".*",true); Test ("Test16","AB",".*",true); Test ("Test17","AAA","aa*",true); Test ("Test18","AAA","AA.A",false); Test ("Test19","AAA","A.A",true); Test ("Test20","AAA",". A",false); Test ("Test21","AAA","A*a",true); Test ("Test22","AAA","Ab*a",false); Test ("Test23","AAA","Ab*ac*a",true); Test ("Test24","AAA","Ab*a*c*a",true); Test ("Test25","AAA",".*",true); Test ("Test26","AaB","C*a*b",true); Test ("Test27","Aaca","Ab*a*c*a",true); Test ("Test28","Aaba","Ab*a*c*a",false); Test ("Test29","Bbbba",". *a*a",true); Test ("Test30","Bcbbabab",". *a*a",false);return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
53-Regular expression matching