天下

记录修行的印记

vc2010 tr1 regexp 小试

#include "stdafx.h"
#include 
<regex>
#include 
<iostream>
#include 
<string>
#include 
<map>
using namespace std;

int main(int argc, char* argv[])
{

    
// 正则替换
    puts("//c++ 0x regex_replace()");
    
string str = "Hello 333world";
    tr1::regex rx(
"\\d+world");
    
string replacement = "planet";
    
string str2 = tr1::regex_replace(str, rx, replacement);
    cout 
<< "result:" << str2 << endl;
    
    
// map + auto
    puts("\r\n//map + auto");
    map
<stringstring> m;
    m[
"aaa"= "a111111";
    m[
"bbb"= "b22222";
    m[
"ccc"= "c33333";
    
for(auto i = m.begin(); i != m.end(); i++
    {
        cout 
<< i->first << i->second << endl;
    }
    
    
// c++ 0x regex_match()
    puts("\r\n//c++ 0x regex_match() ");
    
const regex p("(\\w+) \\w+ (\\d+) \\w+ (\\d+).*");
    
char *= "aaa bbb 333 eee 555";
    cmatch re;
    
bool ret = regex_match(s, re, p);
    
//if (ret)
    {
        
string t;
        
for(auto i = re.begin(); i != re.end(); i++)
        {
            t 
= i->first;
            t 
= t.substr(0, i->second - i->first);
            cout 
<< t << endl;
        }
    }
    
// c++ 0x regex_search()
    puts("\r\n//c++ 0x regex_search()");
    
char *s2 = "hello world. 123";
    regex p2(
"\\d+", tr1::regex_constants::icase);
    cmatch re2;
    regex_search(s2, re2, p2);
    cout 
<< "Matched \""  << re2.str()     
    << "\" after \"" << re2.prefix()     
    
<< "\" at offset: " << re2.position()     
    << " with length: " << re2.length()
    
<< endl; 
    
    
    cout 
<< tr1::regex r"(Hello "world")"


    getchar();

    
return 0;
}

posted on 2012-05-15 14:00 天下 阅读(581) 评论(0)  编辑 收藏 引用 所属分类: Win32


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2011年12月>
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

导航

统计

常用链接

留言簿(4)

随笔分类(378)

随笔档案(329)

链接

最新随笔

搜索

最新评论