读书人

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

发布时间: 2012-08-30 09:55:54 作者: rapoo

使用正则表达式来分割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 Server

热点推荐