NoFear

导航

string匹配(字符串)

Posted on 2012-07-26 15:49  Fear_Hao  阅读(326)  评论(0编辑  收藏  举报
View Code
    string result;
    string str = "uid=1000012(as) gid=1000000(domain users) groups=1000000(domain users),1000015(ABD/users),1000015(ABD/s)";
    int cur_pos = 0;
    int left_pos = 0;
    int right_pos = 0;
    int offset = 0;

    while(1)
    {
        left_pos = str.find('(',cur_pos);
        right_pos = str.find(')',cur_pos);

        if(left_pos == -1 || right_pos == -1)
            break;

        cur_pos = left_pos + 1;
        offset = right_pos - left_pos - 1;

        result = str.substr(cur_pos,offset);
        printf("%s\n",result.c_str());
        cur_pos = right_pos +1;
    }