//Dynamic Planning Public classRegex { Public BooleanIsMatch (String s, String p) {//p length is 0, boundary condition. if(p.length () = = 0) { returnS.length () = = 0; } //p length is 1, boundary condition. if(p.length () = = 1) { //s length is 0 if(S.length () < 1) { return false; } //There are two cases of first element matching//if P is. Then s the first element and P must match, and if the first element of P is the same as the first element of S, it must also match. Else if((P.charat (0)! = S.charat (0)) && (P.charat (0)! = '. ')) { return false; } //otherwise, in addition to the first matching element, compare other elements to the idea of dynamic planning. Else { returnIsMatch (s.substring (1), p.substring (1)); } } //the second element of P is not a *,* representing 0 or more preceding elements if(P.charat (1)! = ' * ') { if(S.length () < 1) { return false; } Else if((P.charat (0)! = S.charat (0)) && (P.charat (0)! = '. ')) { return false; } Else { returnIsMatch (s.substring (1), p.substring (1)); } } Else //the second element of P is * { //* represents 0 elements in front of each other if(IsMatch (S, p.substring (2))) { return true; } //* represents one or more of the preceding elements inti = 0; while(I<s.length () && (S.charat (i) ==p.charat (0) | | P.charat (0) = = '. ')) { if(IsMatch (s.substring (i + 1), p.substring (2))) { return true; } I++; } return false; } } Public Static voidMain (string[] args) {Regex2 reg2=NewRegex2 (); System.out.println (Reg2.ismatch ("Aaba", "Ab*a*c*a")); }}
Regular expression Matching,regex, regex matching, using dynamic programming