使用正则表达式解析一般sql语句(C++)

前端之家收集整理的这篇文章主要介绍了使用正则表达式解析一般sql语句(C++)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

趁这个时候把之前写过的代码都贴上来吧。
正则表达式一用,整个人都感觉神清气爽了,会了正则表达式,表示字符串解析什么的,顿时轻松了不少啊。不过C和C++现存的大部分编译器是不支持正则表达式的,最新出来的C++10标准(貌似是C++10)中新加入了正则表达式,但是支持最新标准的编译器较少,VS最新版本的应该是支持的。不过博主一直对CodeBlocks情有独钟,忠心不二啊。这就得给编译器装boost函数库了, 这是一项浩大且繁琐的工程,以至于现在想想都会痛哭流涕,当时装了一整天,失败了好多次才装好。
如果你时间实在紧迫,或则怕麻烦,那博主还是建议你使用VS或则放弃C++,C投靠JAVA吧,JAVA中可以直接使用正则表达式很方便的。怎么安装正则表达式我就不详细说了,你得先去boost官网下载boost函数库,然后安装到电脑,最后还得配置到编译器上。有很多详细的介绍,还麻烦大家查一查。
当然,安装好boost库后还得学一学怎么使用正则表达式,正则表达式是有单独的语法的,不过不难,几个小时就能学会吧,给大家推荐一个链接,里面介绍得很详细http://www.jb51.net/tools/zhengze.html
学好了之后就可以应用了,其实之前写的计算器如果用正则表达式可能会方便很多,这里用正则表达式来解析部分的sql语句,不过只是较为简单的形式,不支持复杂形式如语句嵌套。

C++代码,这个没有代码注释的习惯我以后一定改….另外,如果大家运行不成功,可能是main函数出了问题,因为main函数之前有修改过,其他两个文件是不会有问题的

//头文件,语句解析类
#ifndef sqlEXPRESSION_H_INCLUDED
#define sqlEXPRESSION_H_INCLUDED

#include <string.h>
#include <vector>
#include <boost/regex.hpp>
#include<algorithm>
#include<iostream>

using namespace std;
using namespace boost;

class sqlEx{
public:
    sqlEx(string ex);
    ~sqlEx();
    bool check();
    vector<vector<string>> getResult();
private:
    bool ForUpdate();
    bool ForDelete();
    bool ForInsert();
    bool ForCreate();
    bool ForSelect();
    bool ForDrop();
    string sql(string str);
    vector<vector<string>> result;
    string Ex;
};


#endif // sqlEXPRESSION_H_INCLUDED
#include <fstream>
#include "sqlexpression.h"

using namespace std;
using namespace boost;

sqlEx::sqlEx(string ex){
    Ex = ex;
}

sqlEx::~sqlEx(){
    //delete Vresults;
}
bool sqlEx::check(){
    transform(Ex.begin(),Ex.end(),Ex.begin(),towlower);
    Ex=sql(Ex);

    Ex=Ex+"END";

    string sqlupdate="update\.+set\.+(where\.+)?END";
    string sqldelete="delete\.+from\.+where\.+END";
    string sqlinsert="insert\.+into\.+(\(\.+/)\.*)?values\(.+\)END";
    string sqlcreate="create table\.+\\(\.+\\)END";
    string sqlselect="select\.+from\.+(where\.+)?((group by)?|(order by)?|(having)?)END";
    string sqldrop  ="drop table\.+END";

    regex Pupdate(sqlupdate);
    regex Pdelete(sqldelete);
    regex Pinsert(sqlinsert);
    regex Pcreate(sqlcreate);
    regex Pselect(sqlselect);
    regex Pdrop(sqldrop);

    if(regex_match(Ex,Pupdate)){
       return  ForUpdate();
    }
    else if(regex_match(Ex,Pdelete)){
       return  ForDelete();
    }
    else if(regex_match(Ex,Pinsert)){
        return ForInsert();
    }
    else if(regex_match(Ex,Pcreate)){
        return ForCreate();
    }
    else if(regex_search(Ex,Pselect)){
        return ForSelect();
    }
    else if(regex_match(Ex,Pdrop)){
        return ForDrop();
    }
    else{
            cout<<"请检查你的语句是否正确,不支持该语句!!"<<endl;
            return false;
    }
    return true;
}

bool sqlEx::ForUpdate(){
    vector<vector<string>> Vupdate;
    string str[3];

    smatch sm[3];
    regex re1("(?<=update )\.+(?= set)");
    regex re2("((?<=set )\.+(?= where))|((?<=set )\.+(?=END))");
    regex re3("(?<=where )\.+(?=END)");

    if(regex_search(Ex,sm[0],re1)){
        vector<string>update1;
        str[0]=sm[0].str();
        update1.push_back(str[0]);
        cout<<"update:"<<endl;
        cout<<str[0]<<endl;
        cout<<endl;
        Vupdate.push_back(update1);
    }else{
        cout<<"请检查update与set语句间是否存在错误!!"<<endl;
        return false;
    }
    if(regex_search(Ex,sm[1],re2)){
            vector<string>update2;
        str[1]=sm[1].str();
         update2.push_back(str[1]);
         cout<<"set:"<<endl;
        cout<<str[1]<<endl;
        cout<<endl;
    Vupdate.push_back(update2);
    }else{
        cout<<"请检查set字段是否存在错误!!"<<endl;

        return false;
    }

    if(regex_search(Ex,sm[2],re3)){
        vector<string>update3;
        cout<<"where:"<<endl;
        str[2]=sm[2].str();
        regex rr("(\.+?and )|(\.+?or )");
        smatch rrsm;
        string::const_iterator st=str[2].begin();
        string::const_iterator en=str[2].end();
        bool flag=false;
        while(regex_search(st,en,rrsm,rr)){
            flag =true;
            cout<<rrsm.str()<<endl;
            update3.push_back(rrsm.str());
            st=rrsm[0].second;
        }
        if(flag){
                regex_search(st,regex("\.+"));
                cout<<rrsm.str()<<endl;
                update3.push_back(rrsm.str());
        }
        else{
                cout<<str[2]<<endl;
                update3.push_back(str[2]);
        }
        Vupdate.push_back(update3);
    }
    result=Vupdate;
    return true;
}

bool sqlEx::ForDelete(){
    vector<vector<string>> Vdelete;

    regex re1("(?<=delete from )\.+(?=where )");
    regex re2("(?<=where )\.+(?=END)");

    string dstr[2];
    smatch dsm[2];

    string::const_iterator st = Ex.begin();
    string::const_iterator en = Ex.end();

    if(regex_search(Ex,dsm[0],re1)){
        vector<string>vdelete1;
        dstr[0]=dsm[0].str();
        vdelete1.push_back(dstr[0]);
        cout<<"delete from:"<<endl;
        cout<<dstr[0]<<endl;
        cout<<endl;
        Vdelete.push_back(vdelete1);
    }else{
        cout<<"检查delete from与where之间是否存在错误"<<endl;
    }
    if(regex_search(Ex,dsm[1],re2)){
        vector<string>vdelete1;
        cout<<"where:"<<endl;

        dstr[1]=dsm[1].str();
        regex rr("(\.+?and )|(\.+?or )");
        smatch rrsm;
        string::const_iterator st=dstr[1].begin();
        string::const_iterator en=dstr[1].end();
        bool flag=false;
        while(regex_search(st,rr)){
            flag =true;
            cout<<rrsm.str()<<endl;
            vdelete1.push_back(rrsm.str());
            st=rrsm[0].second;
        }
        if(flag){
                regex_search(st,regex("\.+"));
                cout<<rrsm.str()<<endl;
                vdelete1.push_back(rrsm.str());
        }
        else{
                cout<<dstr[1]<<endl;
            vdelete1.push_back(dstr[1]);
        }
        Vdelete.push_back(vdelete1);
    }else{
        cout<<"检查where语句后是否存在错误!!"<<endl;
    }
    result=Vdelete;
    return true;
}

bool sqlEx::ForInsert(){
    vector<vector<string>> Vinsert;

    regex re1("(?<=insert into )\.+(?=values)");
    regex re2("(?<=values\\()\.+(?=\\)END)");

    string istr[2];
    smatch ism[2];

    if(regex_search(Ex,ism[0],re1)){
        vector<string>vinsert1;
        istr[0]=ism[0].str();

        regex rr("(?<=\\()\.+(?=\\))");
        smatch rsm;
         cout<<"Insert into:"<<endl;
        if(regex_search(istr[0],rsm,rr)){
            regex rr2("\.+?(?=\\()");
            smatch rsm2;
            regex_search(istr[0],rsm2,rr2);
            cout<<rsm2.str()<<endl;
            cout<<endl;
            vinsert1.push_back(rsm2.str());

            string rstr=rsm.str();

            smatch rsm1;
            regex rr1("(?<=')\.+?(?=')") ;
            string::const_iterator st=rstr.begin();
            string::const_iterator en=rstr.end();

            while(regex_search(st,rsm1,rr1)){
                if(rsm1.str()!=","){
                    cout<<rsm1.str()<<endl;
                    vinsert1.push_back(rsm1.str());
                }
                st=rsm1[0].second;
            }
        }else{
                vinsert1.push_back(istr[0]);
                cout<<istr[0]<<endl;
        }
        Vinsert.push_back(vinsert1);
        cout<<endl;
    }else{
        cout<<"检查insert into语句后是否存在错误!!!"<<endl;
        return false;
    }
    if(regex_search(Ex,ism[1],re2)){
            vector<string>vinsert2;
        istr[1]=ism[1].str();
        cout<<"Values:"<<endl;
        smatch rsm3;
        regex rr3("(?<=')\.+?(?=')") ;
        string::const_iterator st=istr[1].begin();
        string::const_iterator en=istr[1].end();

        while(regex_search(st,rsm3,rr3)){
            if(rsm3.str()!=","){
                cout<<rsm3.str()<<endl;
                vinsert2.push_back(rsm3.str());
            }
            st=rsm3[0].second;
        }
        Vinsert.push_back(vinsert2);
    }else{
        cout<<"检查values语句后是否存在错误!!!"<<endl;
        return false;
    }
    result=Vinsert;
    return true;
}

bool sqlEx::ForCreate(){
    vector<vector<string>> Create;
    regex re1("(?<=create table )\.+?(?=\\()");
    regex re2("(?<=\\()\.+(?=\\))");
     smatch csm1;
     smatch csm2;
     string cstr[2];

     if(regex_search(Ex,csm1,re1)){
        vector<string>create1;
        cstr[0]=csm1.str();
        cout<<"create table:"<<endl;
        cout<<cstr[0]<<endl;
        cout<<endl;
        create1.push_back(cstr[0]);
        Create.push_back(create1);
     }else{
        cout<<"检查create table语句后是否存在错误!!!"<<endl;
        return false;
    }
     if(regex_search(Ex,csm2,re2)){
        cstr[1]=csm2.str();

        regex rer("(\.+?,)|(\.+)");
        smatch rsm;
        string::const_iterator st=cstr[1].begin();
        string::const_iterator en=cstr[1].end();

        while(regex_search(st,rer)){
            string ss=rsm.str();
            bool isname=true;
            bool isatt=false;
            bool iscap=false;
            string name="";
            string attribute="";
            string capacity="";
            vector<string> create1;
            for(int i=0;i<ss.length();i++){
                if(isname==true&&ss[i]!=' '){
                    name = name+ss[i];
                }
                if(isname==true&&name!=""&&ss[i]==' '){
                    isname=false;
                    isatt=true;
                    cout<<name<<endl;
                    create1.push_back(name);
                }
                if(isatt==true&&ss[i]!=' '&&ss[i]!='('){
                    attribute=attribute+ss[i];
                }
                if(isatt==true&&attribute!=""&&ss[i]=='('){
                    isatt=false;
                    iscap=true;
                    cout<<attribute<<endl;
                    create1.push_back(attribute);
                }
                if(iscap==true&&ss[i]!=' '&&ss[i]!='('&&ss[i]!=')'){
                    capacity=capacity+ss[i];
                }
                if(iscap==true&&ss[i]==')'&&capacity!=""){
                    iscap=false;
                    cout<<capacity<<endl;
                    create1.push_back(capacity);
                }
            }
            Create.push_back(create1);
            cout<<endl;
            st=rsm[0].second;
        }
     }else{
            cout<<"检查括号中语句是否存在错误!!!"<<endl;
            return false;
        }
    result=Create;
    return true;
}

bool sqlEx::ForSelect(){
    vector<vector<string>> Select;
    regex reg1("(?<=select )\.+?(?= from)");
    regex reg2("((?<=from )\.+?(?= where))|((?<=from )\.+?(?=END))");
    regex reg3("(?<=where )\.+?(?=END)");

    smatch ssm[3];

    if(regex_search(Ex,ssm[0],reg1)){
            cout<<"select:"<<endl;
        vector<string> attribute;
        string stt="";
        string ss=ssm[0].str();
        for(int i=0;i<ss.size();i++){
            if(ss[i]!=' '&&ss[i]!=','){
                stt=stt+ss[i];
            }
            if((ss[i]==','||i==ss.size()-1)&&stt!=""){
                cout<<stt<<endl;
                attribute.push_back(stt);
                stt="";
            }
        }
        Select.push_back(attribute);
    }else{
            cout<<"检查select from语句后是否存在错误!!!"<<endl;
            return false;
        }
    if(regex_search(Ex,ssm[1],reg2)){
            cout<<"from:"<<endl;
        vector<string> tablename;
        string stt="";
        string ss=ssm[1].str();
        for(int i=0;i<ss.size();i++){
            if(ss[i]!=' '&&ss[i]!=','||i==ss.size()-1)&&stt!=""){
                cout<<stt<<endl;
                tablename.push_back(stt);
                stt="";
            }
        }
        Select.push_back(tablename);
    }else{
            cout<<"检查from语句后是否存在错误!!!"<<endl;
            return false;
        }
    if(regex_search(Ex,ssm[2],reg3)){
        vector<string>condition;
        cout<<"where:"<<endl;
        string sst =ssm[2].str();
        regex sr("(\.+?and )|(\.+?or )");
        smatch srsm;
        string::const_iterator st=sst.begin();
        string::const_iterator en=sst.end();
        bool flag=false;
        while(regex_search(st,srsm,sr)){
            flag =true;
            cout<<srsm.str()<<endl;
            condition.push_back(srsm.str());
            st=srsm[0].second;
        }
        if(flag){
                regex_search(st,regex("\.+"));
                cout<<srsm.str()<<endl;
                condition.push_back(srsm.str());
        }
        else{
                cout<<sst<<endl;
                condition.push_back(sst);
        }
        Select.push_back(condition);
    }
    result=Select;
    return true;
}

bool sqlEx::ForDrop(){
    vector<vector<string>>drop;
    regex dre("(?<=drop table )\\w+(?=END)");
    smatch dsm;
    cout<<"drop table:"<<endl;

    if(regex_search(Ex,dsm,dre)){
        cout<<dsm.str()<<endl;
        vector<string> name;
        name.push_back(dsm.str());
        drop.push_back(name);
    }else{
            cout<<"检查drop语句后是否存在错误!!!"<<endl;
            return false;
        }

    result=drop;
    return true;
}

vector<vector<string>>sqlEx::getResult(){
    return result;
}

string sqlEx::sql(string sql)//语句预处理
{
        const int count=sql.size();
        string b="";
        int flag=0;//标记
        int n=0;
        for(int i=0;i<count;i++)
        {
                if(sql[i]!=' ')
                {
                        b+=sql[i];
                        n++;
                        flag=1;
                }
                else if(sql[i]==' '&&flag==1)
                {
                        b+=sql[i];
                        n++;
                        flag=0;
                }
        }
        if(b[n-1]==' ')
              b.erase(n-1,1);
        return b;
}
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

#include "sqlexpression.h"

//#define other

int main()
{
    #ifndef other
    std :: string str="";
    do{
        getline(std :: cin,str);
        sqlEx se(str);
        if(se.check()){
            vector vec = null;
                    string vec=se.getResult();                     for(int i=0;i<vec.size();i++){
                for(int j=0;j<vec[i].size();j++){
                    cout<<vec[i][j]<<endl;
                }
                cout<<endl;
            }*/
        }
    }while(str!="#");



    return 0;
}
原文链接:https://www.f2er.com/regex/360063.html

猜你在找的正则表达式相关文章