Boost.Regex库使用实例

01.#include <iostream>
02.#include <boost/regex.hpp>
03. 
04.using namespace std;
05.int main(int argc, char* argv[])
06.{    //( 1 )   ((  3  )  2 )((  5 )4)(    6    )   
07.    //(/w+)://((/w+/.)*/w+)((//w*)*)(//w+/./w+)?
08.    //^协议://网址(x.x...x)/路径(n个/字串)/网页文件(xxx.xxx)
09.    const char *szReg = "(//w+)://((//w+//.)*//w+)((///w*)*)(///w+//.//w+)?";
10.    const char *szStr = "http://www.cppprog.com/2009/0112/48.html";
11. 
12.    {    //字符串匹配
13.        boost::regex reg( szReg );
14.        bool r=boost::regex_match( szStr , reg);
15.        assert(r);
16.    }
17. 
18.    {    //提取子串
19.        boost::cmatch mat;
20.        boost::regex reg( szReg );
21.        bool r=boost::regex_match( szStr, mat, reg);
22.        if(r) //如果匹配成功
23.        {
24.            //显示所有子串
25.            for(boost::cmatch::iterator itr=mat.begin(); itr!=mat.end(); ++itr)
26.            {
27.                //       指向子串对应首位置        指向子串对应尾位置          子串内容
28.                cout << itr->first-szStr << ' ' << itr->second-szStr << ' ' << *itr << endl;
29.            }
30.        }
31.        //也可直接取指定位置信息
32.        if(mat[4].matched) cout << "Path is" << mat[4] << endl;
33.    }
34. 
35.    { //查找
36.        boost::cmatch mat;
37.        boost::regex reg( "//d+" );    //查找字符串里的数字
38.        if(boost::regex_search(szStr, mat, reg))
39.        {
40.            cout << "searched:" << mat[0] << endl;
41.        }
42.    }
43. 
44.    { //替换
45.        boost::regex reg( szReg );
46.        string s = boost::regex_replace( string(szStr), reg, "ftp://$2$5");
47.        cout << "ftp site:"<< s << endl;
48.    }
49.    { //替换2,把<>&转换成网页字符
50.        string s1 = "(<)|(>)|(&)";
51.        string s2 = "(?1<)(?2>)(?3&)";
52.        boost::regex reg( s1 );
53.        string s = boost::regex_replace( string("cout << a&b << endl;"), reg, s2, boost::match_default | boost::format_all);
54.        cout << "HTML:"<< s << endl;
55.    }
56. 
57.    { //使用迭代器找出所有数字
58.        boost::regex reg( "//d+" );    //查找字符串里的数字
59.        boost::cregex_iterator itrBegin = make_regex_iterator(szStr,reg); //(szStr, szStr+strlen(szStr), reg);
60.        boost::cregex_iterator itrEnd;
61.        for(boost::cregex_iterator itr=itrBegin; itr!=itrEnd; ++itr)
62.        {
63.                //       指向子串对应首位置        指向子串对应尾位置          子串内容
64.                cout << (*itr)[0].first-szStr << ' ' << (*itr)[0].second-szStr << ' ' << *itr << endl;
65.        }
66.    }
67. 
68.    { //使用迭代器拆分字符串
69.        boost::regex reg("/");  //按/符拆分字符串
70.        boost::cregex_token_iterator itrBegin = make_regex_token_iterator(szStr,reg,-1); //使用-1参数时拆分,使用其它数字时表示取第几个子串,可使用数组取多个串
71.        boost::cregex_token_iterator itrEnd;
72.        for(boost::cregex_token_iterator itr=itrBegin; itr!=itrEnd; ++itr)
73.        {
74.            cout << *itr << endl;
75.        }
76.    }
77. 
78.    { //使用迭代器拆分字符串2
79.        boost::regex reg("(.)/(.)");  //取/的前一字符和后一字符(这个字符串形象貌似有点邪恶-_-)
80.        int subs[] = {1,2};        // 第一子串和第二子串
81.        boost::cregex_token_iterator itrBegin = make_regex_token_iterator(szStr,reg,subs); //使用-1参数时拆分,使用其它数字时表示取第几个子串,可使用数组取多个串
82.        boost::cregex_token_iterator itrEnd;
83.        for(boost::cregex_token_iterator itr=itrBegin; itr!=itrEnd; ++itr)
84.        {
85.            cout << *itr << endl;
86.        }
87.    }
88. 
89. 
90.    cin.get();
91.    return 0;
92.}

 转自:http://blog.csdn.net/ghlfllz/article/details/6299846

posted @ 2013-02-18 13:52  stma  阅读(410)  评论(0)    收藏  举报