Question 53: Regular expression matching Java

Source: Internet
Author: User
Tags first string

introduction: This problem is more complex, boundary conditions are more, in order to facilitate the review, Collation. also, because C and Java have different operations for strings, there are changes to the Code.

title: please implement a function to match the containing '. ' And the regular expression of ' * '. The characters in the pattern '. ' Represents any character, and ' * ' indicates 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 "ab*ac*a", but does not match "aa.a" and "ab*a".

Analysis: The core of this problem is actually analysis ' * ', for '. ' , it matches any character and can be used as a normal character. For the ' * ' analysis, we have to discuss the situation, when all the situation is clear, you can write Code.

In each round of matches, thesecond character of Patttern is ' * ' when:

    1. The first character does not match ('. ') Matches any character), then ' * ' can only represent a match 0 times, such as ' ba ' and ' A*ba ', the string is unchanged, the pattern moves backwards by two characters, and then matches the remaining string and pattern
    2. The first character matches, then ' * ' may represent a match 0 times, 1 times, multiple times, such as ' AAA ' and ' a*aaa ', ' aba ' and ' A*ba ', ' Aaaba ' and ' A*ba '. Match 0 times, The string does not change, the pattern moves backwards by two characters, and then matches the remaining string and pattern, the string moves backward by one character when matched 1 times, the pattern moves backwards by 2 characters, The string moves back one character at a time, and the pattern remains unchanged;

And when the second character of Patttern is not ' * ' , the situation is much simpler :

    1. If the first character of the string matches the first character in the pattern, one character is moved backwards in both the string and the pattern, and then the remaining strings and patterns are Matched.
    2. If the first character of the string does not match the first character in the pattern, it returns false directly.

ok, now the idea is clear, you can look at the code:

1  packagetest;2 Importjava.util.Scanner;3 4  public classquestion_53 {5      public Static BooleanMatch (String input,string Pattern) {6         if(input==NULL|| pattern==NULL)return false;7         returnMatchcore (input,0,pattern,0);8     }9     Private Static BooleanMatchcore (String input,intI,string pattern,intP) {Ten         if((input.length () ==i) && (pattern.length () = =p)) { one             //exit 1,input and pattern are at the end of the string a             return true; -         } -         if((i!=input.length ()) && (pattern.length () = =p)) { the             //exit 2, string input not to end, pattern to the end -             return false; -         } -         if((input.length () ==i) && (pattern.length ()! =p)) { +             //exit 3, string input to end, pattern not to end -             return false;  +         } a          at         if((p+1<pattern.length ()) && (pattern.charat (p+1) = = ' * ')) {//pattern a second character is * -             if((input.charat (i) ==pattern.charat (p)) | | (pattern.charat (p) = = '. ')){ -                 //match the first letter -                 returnMatchcore (input,i+1,pattern,p+2)//* Indicates a 1-time occurrence -|| Matchcore (input,i+1,pattern,p)//* indicates multiple occurrences -|| Matchcore (input,i,pattern,p+2);//* indicates 0 times, a ... p* ... in}Else{ -                 //initials do not match to                 returnMatchcore (input,i,pattern,p+2); +             } -}//end Pattern.charat (p+1) = = ' * ' the          *         if((input.charat (i) ==pattern.charat (p)) | | (pattern.charat (p) = = '. ')){ $             //pattern The second letter is not a *, and the first letter matchesPanax Notoginseng             returnMatchcore (input,i+1,pattern,p+1); -         } the         return false;//all the rest of the situation doesn't match . +     } a  the      public Static voidmain (string[] Args) { +         //TODO auto-generated Method Stub -Scanner Scanner =NewScanner (system.in);//Scan Keyboard Input $System.out.println ("please Enter the first string:");  $String str1 =Scanner.nextline ();  -System.out.println ("please Enter a second string:");  -String str2 =Scanner.nextline (); the scanner.close (); -          WuyiSystem.out.print ("the result of the match is:");  the System.out.println (match (str1, str2));  -     } wu  -}

Note the boundary condition, when both the pattern and the string are only one character, pattern.charat (p+1) = = ' * ' will be in the range, so you need to add additional restrictions p+1<pattern.length () , This condition can not be satisfied will enter the next if judgment statement, directly determine whether two characters are equal, and finally enter the exit 1, return True. Either the string and the pattern end are considered mismatched and return false, which is exit 2 and Exit 3.

Question 53: Regular expression matching Java

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.