Implementation of the regular expression * and? The

Source: Internet
Author: User

A few days ago when using grep, found that grep's * incredibly support problems, and can not get the desired results, so took some time to implement the regular expression x match, the following function can match *,?。

The code is short, but it's efficient.

BOOL Match_star (const char* text,const char* pattern) {const char *CP = text;        Const char* PP = pattern;        const char *PS1, *PS2;        if (!*pattern) return true;                while (*CP) {PS1 = CP;                PS2 = PP;                                while (*ps1 && *ps2) {if (*PS2 = = ' * ') {                                CP = Ps1-1;                                pp = ps2+1;                        Break } else if (0 = = (*PS1-*PS2) | | *ps2 = = '? ')                                {ps1++;                        ps2++;                        } else {break;                }} if (!*PS2) {return true;        } cp++; } return false;} 


Test passes on Windows VC2010 and Linux g++.

When the match X is implemented, a technique is used to compare two new strings and discard what has already been compared.

For example, the string is 1234567890, the pattern string is 1*23*0 and 1? 3 can match the success.

If there is a problem, please correct me, thank you!



Implementation of the regular expression * and? The

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.