• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            lzm

            who dare win.
            posts - 14, comments - 29, trackbacks - 0, articles - 0

            (正則表達式)是否匹配(字符串)

            Posted on 2009-04-04 22:58 lzmagic 閱讀(3946) 評論(3)  編輯 收藏 引用 所屬分類: Algorithm
            /**
             * IS_MATCHED
             * 輸入:(1)一個正則表達式:帶有正則符號'*'、'+'、'?'、'.'(正則符號不相鄰); 
             *          '*':任意多個任意字符; 
             *          '+':至少1個任意字符; 
             *          '?':0個或1個任意字符; 
             *          '.':恰好1個任意字符。                                                                 
             *       (2)一個字符串。 
             * 輸出:該正則表達式是否匹配該字符。 
             
            */

            #include 
            <iostream>
            #include 
            <string>
            #include 
            <algorithm>
            using namespace std;

            bool Is_Matched(string &reg, string &str)
            {
                 
            bool is_asterisk, is_plus, is_question, is_dot, is_ok(true);
                 
            string substr;
                 
            string::iterator reg_it(reg.begin()), str_it(str.begin()), str_it2;
                 
            while (is_ok && reg_it != reg.end())
                 
            {
                     
            // 分離正則符號給is_asterisk、is_plus、is_question、is_dot。 
                     is_asterisk = *reg_it == '*';
                     is_plus     
            = *reg_it == '+';
                     is_question 
            = *reg_it == '?';
                     is_dot      
            = *reg_it == '.';
                     
            if ( is_asterisk || is_plus || is_question || is_dot) ++reg_it;
                     
            // 分離子字符串給substr。 
                     substr.clear();
                     
            for (reg_it = reg_it; reg_it != reg.end()
                                       
            && *reg_it != '*'
                                       
            && *reg_it != '+'
                                       
            && *reg_it != '?'
                                       
            && *reg_it != '.'++reg_it)// 調整reg_it。 
                         substr.push_back(*reg_it);
                     
            // 匹配子字符串:substr空,str_it2=>end(),否則str_it2=>匹配點或end()。 
                     if (substr.empty()) str_it2 = str.end();
                     
            else str_it2 = search(str_it, str.end(), substr.begin(), substr.end());
                     
            // 是否匹配正則符號:is_asterisk不做判斷。 
                     if (is_plus     && str_it ==  str_it2  
                      
            || is_question && str_it != str_it2 && str_it != str_it2 - 1
                      
            || is_dot      && str_it != str_it2 - 1) is_ok = false;
                     
            // 是否匹配子字符串。 
                     if (!substr.empty() && str_it2 == str.end()) is_ok = false;
                     
            else str_it = str_it2 + substr.size();       // 調整str_it。 
                 }

                 
            // 匹配成功:is_ok為true,reg_it到達reg.end(),str_it到達str.end()。 
                 return is_ok && reg_it == reg.end() && str_it == str.end();
            }


            int main () 
            {
                
            string reg, str; // input : *l?m.g+ lzmagic ?zm lzmagic
                while (cin >> reg >> str)
                
            {
                      
            if (Is_Matched(reg, str)) cout << "match" << endl;
                      
            else cout << "dismatch" << endl;
                }

                
                system(
            "pause");
                
            return 0;
            }

            Feedback

            # re: IS_MATCHED 正則表達式是否匹配字符串  回復  更多評論   

            2009-04-05 11:44 by 陳梓瀚(vczh)
            再接再厲,做個支持(ab?|bc)*的。

            # re: [字符串處理] IS_MATCHED (正則表達式)是否匹配(字符串)  回復  更多評論   

            2009-04-06 11:23 by lzmagic
            @陳梓瀚(vczh)
            嘻嘻,不會~

            # re: (正則表達式)是否匹配(字符串)[未登錄]  回復  更多評論   

            2011-08-02 10:54 by xyz
            呃……請問為什么我輸入A*G.C和AGTGTC,結果是dismatch呢?
            免费观看成人久久网免费观看| 国产美女久久精品香蕉69| 久久精品成人免费网站| 国产婷婷成人久久Av免费高清| 91精品婷婷国产综合久久| 久久久精品日本一区二区三区| 欧美国产精品久久高清| 色欲久久久天天天综合网 | 国产巨作麻豆欧美亚洲综合久久| 99久久免费国产精品| 精品国产青草久久久久福利| 亚洲国产成人久久综合一| 亚洲国产精品成人久久蜜臀| 精品精品国产自在久久高清| 中文字幕精品久久| 99久久亚洲综合精品网站| 亚洲女久久久噜噜噜熟女| 久久久久国产亚洲AV麻豆| 亚洲av伊人久久综合密臀性色| 国产视频久久| 91精品国产综合久久香蕉| 精品熟女少妇a∨免费久久| 久久天天躁夜夜躁狠狠| 亚洲精品99久久久久中文字幕| 亚洲国产精品热久久| 精品久久久久久中文字幕人妻最新| 伊人久久大香线蕉无码麻豆| 国产精品九九久久免费视频| 日本久久久久久中文字幕| 久久国产一区二区| 久久精品一区二区| 亚洲国产精品久久久久久| 欧美日韩中文字幕久久伊人| 久久丫精品国产亚洲av| 人妻少妇久久中文字幕一区二区| 伊人热热久久原色播放www| 中文字幕亚洲综合久久菠萝蜜| 久久综合鬼色88久久精品综合自在自线噜噜 | 久久96国产精品久久久| 国产精品无码久久久久久| 少妇内射兰兰久久|