**regular Expression Match

Source: Internet
Author: User

Implement regular expression matching with support for ‘.‘ and ‘*‘ .

‘.‘ Matches any single character. ' * ' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be:bool IsMatch (const char *s, const char *p) Some examples:ismatch ("AA", "a") →falseismatch ( "AA", "AA") →trueismatch ("AAA", "AA") →falseismatch ("AA", "A *") →trueismatch ("AA", ". *") →trueismatch ("AB", ". *") →true IsMatch ("AaB", "C*a*b") →true


title :

Match the regular expression, '. ' Can match any one character, ' * ' before must have a character, two combined to indicate that the previous character appears 0 to infinity.

Solution :

Be sure to note that ' * ' must be used in conjunction with the preceding characters.

This article and code references from: http://harrifeng.github.io/algo/leetcode/regular-expression-matching.html
    • The first thing to understand is test instructions:
      • "A" corresponds to "a", this match does not explain
      • Any letter corresponding to ".", which is also regular common
      • 0 to more than the same character X, corresponding to "x*", compared to normal, this place more than a prefix x. x represents the same character to take one, such as "Aaaab" corresponds to "a*b"
      • "*" There is also an easy way to neglect is that its "greed" to have a limit. For example "AAA" corresponds to "A*a", code logic can not be greedy all the way to the end
    • Regular expressions It is very unrealistic to expect a character to be matched by a character. and the "match" problem is very easy to convert to "match a part", the whole match does not match, depends on "the remaining match" situation. It's good to turn a big problem into a smaller problem: recursion
    • After determining recursion, using Java to implement this problem, you will encounter a lot of different from the C, because Java control of the character is not as flexible as the C language pointer Charat must be sure that a location exists before it can be used.
    • If pattern is a "x*" type, then the pattern is reduced by two two. Otherwise, it is a reduction of one. No matter how you reduce it, make sure there are so many pattern. For example, S.substring (n), where n is the largest and s.length ()
The code is as follows
 Public Static BooleanIsMatch (String s, String p) {if(p.length () = = 0)            returnS.length () = = 0; //Length = = 1 is the case, which is easy to forget. //as P is subtracted 2 each time, so if original//p is odd and then finally it'll face the length 1        if(p.length () = = 1)            return(s.length () = = 1) && (p.charat (0) = = S.charat (0) | | P.charat (0) = = '. '); //next char isn't ' * ': must match current character        if(P.charat (1)! = ' * ') {            if(s.length () = = 0)                return false; Else                return(S.charat (0) = = P.charat (0) | | P.charat (0) = = '. ')                        && IsMatch (s.substring (1), p.substring (1)); }Else{            //Next char is *             while(s.length () > 0 && (p.charat (0) = = S.charat (0) | | P.charat (0) = = '. ')) {                if(IsMatch (S, p.substring (2)))                     return true; S= s.substring (1); }            returnIsMatch (S, p.substring (2)); }    }

Reference:http://www.cnblogs.com/springfor/p/3893593.html

**regular Expression Match

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.