"Sword refers to offer"--matching regular expressions to __ regular expression matching

Source: Internet
Author: User

T:

Topic Description:
Please implement a function to match the include '. ' and ' regular expressions. The characters in the pattern '. ' Represents any one character, and ' means that the preceding character can appear any time (including 0 times). In this point, a match is a string that matches all the characters of the entire pattern. For example, the string "AAA" matches the schema "A.A" and "ab*ac*a", but does not match "aa.a" and "Ab*a"

The first two days just read about regular expression of the basic grammar and usage, today, brush cattle, it happened to meet this problem.
The most convenient way to solve the problem is recursion. Without recursion, the entire process and the situation to classify the discussion are more complex.

According to the description of the topic, the most intuitive point is how to deal with the character '. ' and ' * ' in the same situation, that is, '. * '.

Regardless of how recursion is done, it's always about starting at the left of the string and the schema. Set the following recursive function:

    IsMatch (char[] str, int indexstr, char[] pattern, int indexpat) {...}

The recursive function means: for the string str str that starts with the Indexstr indexstr subscript, and the pattern patterns that begin with the Indexpat Indexpat subscript, the right-hand part of the two can be matched.

For a situation with ' * ', there are 3 cases of the following:

For the first two, when matching, you can have the following matching methods:

In which, the match is repeated, referring to the contents of the "a" above, also using the following two characters '. ' Or ' a ' to match.

For the third case, ' a ' corresponds to ' b* ', this time can be directly ignored ' b* ', that is, matching 0 times.

Put the code:

    /** * T: Regular expression matching * Please implement a function to match the inclusion of '. ' and a regular expression of ' * '. The characters in the pattern '. ' 
 Represents any one character, and ' * ' means that the preceding character can appear any time (including 0 times). * In the subject, the match refers to all characters of the string matching the entire pattern.

    For example, the string "AAA" matches the schema "A.A" and "ab*ac*a", but does not match "aa.a" and "ab*a" * * date:2016.5.20 * @author SSS */public class Solution {
    Public boolean match (char[] str, char[] pattern) {return This.ismatch (str, 0, pattern, 0); /** * Recursive Implementation * @param str * @param indexstr * @param pattern * @param indexpat * @retur N/Public Boolean ismatch (char[] str, int indexstr, char[] pattern, int indexpat) {//both reached the end, indicating that the complete horse
        with if (Indexstr = = Str.length && Indexpat = = pattern.length) {return true;
        }//Template index has exceeded its own length, then it is unmatched if (Indexpat >= pattern.length) {return false;
            //Consider the situation in the pattern containing ' * ' if (Indexpat < pattern.length-1 && Pattern[indexpat + 1] = = ' * ') { if (Indexstr < str.length &&amP (Str[indexstr] = = Pattern[indexpat] | | | pattern[indexpat] = = '. ') {return IsMatch (str, INDEXSTR, pattern, Indexpat + 2) | | ismatch (str, INDEXSTR + 1, pattern, Indexpat + 2 ) ||
            IsMatch (str, INDEXSTR + 1, pattern, indexpat); else {//INDEXSTR reached the end, but Indexpat did not reach the end, and for a in STR, it is possible that the pattern is followed by b* return IsMatch (str, INDEXSTR, patter
            N, Indexpat + 2);
        } if (indexstr = = Str.length) {return false; } if (str[indexstr] = = Pattern[indexpat] | | | pattern[indexpat] = = '. ')
        {return IsMatch (str, INDEXSTR + 1, pattern, Indexpat + 1);
    return false;
    }//public static void Main (String []args) {//char[] str = {' A ', ' d ', ' B ', ' C ', ' B ', ' d '};
    Char[] pattern = {' A ', '. ', ' * ', ' B ', '. ', ' * ', ' d ', ' * '};
    Solution1 Solution1 = new Solution1 ();
    SYSTEM.OUT.PRINTLN (Solution1.match (str, pattern)); //  }
}

Among them, this method is also not what I thought out, reference to the discussion area inside Daniel's practice, this method is understood, according to gourd painting scoop.

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.