使用正则表达式来分割sql语句!

int CSQLMake::SplitSql(std::string &strSql, std::vector<std::string> &vctSql)
{
    int iRet = 0;
    boost::regex regEx("\\(select[\\w\\s='<>!#,.@]*\\)");
    std::string::const_iterator start, end;

    boost::match_results<std::string::const_iterator> what; 
    boost::match_flag_type flags = boost::match_default;

    int iCount = -1;
    while (boost::regex_search(strSql, what, regEx, flags))
    {
        std::string strTemp(what[0].first, what[0].second);
        vctSql.push_back(strTemp);
        iCount ++;

        std::string strFlag = "@@" + toString(iCount);

        std::string::size_type stPos = strSql.find(what[0]);
        std::string strLeft = strSql.substr(0, stPos);
        std::string strRight = strSql.substr(stPos + (what[0].second - what[0].first));

        strSql = strLeft + strFlag + strRight;

        flags |= boost::match_prev_avail;
        flags |= boost::match_not_bob;
    }

    vctSql.push_back(strSql);
    return iRet;
}

你可能感兴趣的:(sql,正则表达式)