Regular expression Matching,regex, regex matching, using dynamic programming

Source: Internet
Author: User

//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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.