Sword Point offer series 26--Regular expression matching

Source: Internet
Author: User

"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

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.