三十行C程式碼做regular expression的match

三十行C程式碼做regular expression的match – 網站服務開發 – Inside 論壇

出處:http://www.cs.princeton.edu/cour … s333/beautiful.html
/* match: search for regexp anywhere in text */
    int match(char *regexp, char *text)
    {
        if (regexp[0] == ‘^’)
            return matchhere(regexp+1, text);
        do {    /* must look even if string is empty */
            if (matchhere(regexp, text))
                return 1;
        } while (*text++ != “);
        return 0;
    }

    /* matchhere: search for regexp at beginning of text */
    int matchhere(char *regexp, char *text)
    {
        if (regexp[0] == “)
            return 1;
        if (regexp[1] == ‘*’)
            return matchstar(regexp[0], regexp+2, text);
        if (regexp[0] == ‘$’ && regexp[1] == “)
            return *text == “;
        if (*text!=" && (regexp[0]==’.’ || regexp[0]==*text))
            return matchhere(regexp+1, text+1);
        return 0;
    }

    /* matchstar: search for c*regexp at beginning of text */
    int matchstar(int c, char *regexp, char *text)
    {
        do {    /* a * matches zero or more instances */
            if (matchhere(regexp, text))
                return 1;
        } while (*text != " && (*text++ == c || c == ‘.’));
        return 0;
    }

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *

What is 15 + 2 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)