Wildcardmatching and regex, wildcard matching and regular expression matching

Source: Internet
Author: User

Wildcardmatching: Wildcard Match

Algorithm Analysis:

1. Two pointer I, J points to the string, the matching formula respectively.

2. If matched, advance directly with 2 pointers.

3. If the matching formula is *, match in the string.

Note the location where the previous comparison was recorded

Implement wildcard pattern matching with support for and ‘?‘‘*‘

‘?‘ Matches any single character.

' * ' Matches any sequence of characters (including the empty sequence).

Some examples:ismatch ("AA", "a") →falseismatch ("AA", "AA") →trueismatch ("AAA", "AA") →falseismatch ("AA", "*") → Trueismatch ("AA", "A *") →trueismatch ("AB", "? *") →trueismatch ("AaB", "C*a*b") →false
public class Wildcardmatching {public Boolean isMatch (string s, String p) {if (s = = NULL | | p = = NULL)        {return false;        } int indexs = 0;                int indexp = 0;        int lenS = S.length ();                int LENP = P.length ();         int PreP = -1;//Record wildcard as * when wildcard string subscript int preS = -1;//record wildcard to match string subscript while (Indexs < LenS) { Non-* matching, indexs and indexp simultaneously move if (Indexp < LENP && (P.charat (indexp) ==s.charat (indexs) | |             P.charat (indexp) = = '? '))                {indexp++;            indexs++;             }//Touch *, record at this time the subscript else if P,s (Indexp < LENP && P.charat (indexp) = = ' * ')                {PreS = Indexs;                                PreP = Indexp;            P Compare pointer back move, assuming * match empty string indexp++;          } else if (PreP! =-1)//try to match * {      Indexp = prep;//p fallback to * position indexp++;            pres++;//try to match *, move one character at a time, then compare P's * Trailing characters and s remaining characters, if not matched, fall back to the position of * Indexs = PreS;            } else {return false;                }} while (Indexp < LENP) {if (P.charat (indexp)! = ' * ') {            return false;        } indexp++;    } return true; }//through examples AaB and *ab to simulate the process public static void main (string[] args) {wildcardmatching wm = new wildcardmatching (); System.out.println (Wm.ismatch ("AaB", "*ab"));}}



Regex: Regular expression matching

Problem Description: 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

Algorithm analysis:. * can match any string, such as AB match. *, not to say let. Match A and then go to match *, but * match is., i.e. (. *) = = (...), so. * Matches all strings.

Using dynamic programming, for match string p, we discuss three cases, p length is 0,p length is 1,p length is greater than 1 (P's second string is *,p second string is not *)

Dynamic planning public class Regex2 {public Boolean isMatch (string s, String p) {//P length is 0, boundary condition. if (p.length () = = 0) {return s.length () = = 0;}//P length is 1, boundary condition.        if (p.length () = = 1) {//S length is 0if (S.length () < 1) {return false;} There are two cases of first element matching//if P is. Then 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 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 {return IsMatch (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 {return IsMatch (s.substring (1), p.substring (1));}} Else//p's second element is that *{//* represents 0 preceding elements if (IsMatch (S, p.substring (2)) {return true;}//* represents one or more preceding elements//traversal s, if s's I element equals P's first element, or the first element of P is., the string that matches the third element of S i+1 and p for (int i = 0; I<s.length () && (S.charat (i) = = P.charat (0) | | (P.charat (0) = = '. ')); i + +) {if (IsMatch (s.substring (i + 1), p.substring (2))) {return true;}}return false;}} public static void Main (string[] args) {Regex2 reg2 = new Regex2 (); System.out.println (Reg2.ismatch ("Aaba", ". *"));}}

Wildcardmatching and regex, wildcard matching and 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.