"topic" Please implement a function to match including '. ' And the regular expression of ' * '. The characters in the pattern '. ' Represents any character, and '' means that the character before it can appear any time (including 0 times). In the subject, the match refers to all characters of the string that match the entire pattern. For example, the string "AAA" matches the pattern "A.A" and "ABaca", but does not match "AA.A" and "Aba"
1 PackageCom.exe6.offer;2 /**3 * "topic" Please implement a function to match including '. ' And the regular expression of ' * '. The characters in the pattern '. ' Represents any one character,4 * and ' means that the characters in front of it can appear any time (including 0 times). In the subject, the match refers to all characters of the string that match the entire pattern. 5 * For example, the string "AAA" matches the pattern "a.a" and "abaca", but does not match "AA.A" and "ABA"6 * @authorWGS7 *8 */9 Public classmatchregstring {Ten One Public BooleanMatchChar[] str,Char[] pattern) { A if(str==NULL|| pattern==NULL) - return false; - returnMatchcore (str,0,str.length,pattern,0, pattern.length); the } - - Public BooleanMatchcore (Char[] str,intIintLength1,Char[] pattern,intJintlength2) { - if(I==length1 && j==length2) { + return true; - /*if (j==length2 | | pattern[j]== ' * ') { + return true; A }else{ at return false; - }*/ - } - if(I!=length1 && j==length2) { - return false; - } in //3 When the next character of the pattern has ' * ' - if(J+1<length2 && pattern[j+1]== ' * '){ to //①a A and a A * A is the same as the value in str to compare + //A a and a. * A - if(str[i]==Pattern[j]) { the //move back two bits * returnMatchcore (str,i+1,length1,pattern,j+2, Length2) $ //in the original statePanax Notoginseng|| Matchcore (str,i+1, Length1,pattern,j,length2) - //Ignore the|| Matchcore (str,i+1,length1,pattern,j+2, length2); + } A //②a A and a B * A that is, the value in STR is different from the values to be compared, pattern ignores the value of the * before, moves back two bit to continue the comparison, the STR value is unchanged the Else{ + returnMatchcore (str,i,length1,pattern,j+2, length2); - } $ } $ - //1 2 The current character matches or matches '. ' Both are shifted right one continues to compare; - if(I<length1 && (Str[i]==pattern[j] | | pattern[j]== '. ')){ the returnMatchcore (str,i+1,length1,pattern,j+1, length2); - }Wuyi return false; the } - Wu Public Static voidMain (string[] args) { - Char[] str=New Char[]{' A ', ' a ', ' a '}; About Char[] pattern=New Char[]{' A ', ' B ', ' * ', ' a ', ' C ', ' * ', ' a '}; $Matchregstring m=Newmatchregstring (); - Booleanb=M.match (str, pattern); - System.out.println (b); - A } + the}
Sword Point offer series 26--Regular expression matching