想用正则表达式来分析语法,实现一个小应用,如 下边我以分析SQL语句为例语句示例:
select * from table
select * from table where x=y
select * from table order by x
select y from table group by x,y
select * from table where x=y order by x desc
select y from table where x=y group by x,y order by y desc//这句是标准的SQL全语句,没有嵌套的情况,把每个子句取出来,如果有嵌套的话,则话在子句中,暂时不理它
select y from table where x=y group by x,y having(y<10) order by y desc下边是最后一句的结果样例:全: select y from table where x=y group by x,y having(y<10) order by y desc
select: select y from table
where: x=y
group: x,y having(y<10)
order: y desc
我对正则不是很懂,经常晕的,嵌套我暂时先不举例,等先把上边的分析出来了再说,先谢谢大家

解决方案 »

  1.   


    private void button1_Click(object sender, EventArgs e)
    {
        string s=@"select * from table
    select * from table where x=y
    select * from table order by x
    select y from table group by x,y
    select * from table where x=y order by x desc
    select y from table where x=y group by x,y order by y desc
    select y from table where x=y group by x,y having(y<10) order by y desc";
        StringBuilder sb=new StringBuilder();
        MatchCollection mc = Regex.Matches(s, @"(?in)(?<select>select\s+.*?\s+from\s+\S+)(\s+where\s+(?<where>((?!\s+(order|group)\s+by\s+).)+))?(\s+group\s+by\s+(?<group>((?!\s+order\s+by\s+).)+))?(\s+order\s+by\s+(?<order>.+))?");
        foreach (Match m in mc)
        {
            sb.AppendLine("语句:" + m.Value);
            sb.AppendLine("==============================================================");
            sb.AppendLine("select:" + m.Groups["select"].Value);
            sb.AppendLine("where:" + m.Groups["where"].Value);
            sb.AppendLine("group:" + m.Groups["group"].Value);
            sb.AppendLine("order:" + m.Groups["order"].Value);
            sb.AppendLine();
        }
        richTextBox1.Text = sb.ToString();
    }运行结果:语句:select * from table
    ==============================================================
    select:select * from table
    where:
    group:
    order:
    语句:select * from table where x=y ==============================================================
    select:select * from table
    where:x=y group:
    order:
    语句:select * from table order by x ==============================================================
    select:select * from table
    where:
    group:
    order:x 
    语句:select y from table group by x,y ==============================================================
    select:select y from table
    where:
    group:x,y order:
    语句:select * from table where x=y order by x desc ==============================================================
    select:select * from table
    where:x=y
    group:
    order:x desc 
    语句:select y from table where x=y group by x,y order by y desc ==============================================================
    select:select y from table
    where:x=y
    group:x,y
    order:y desc 语句:select y from table where x=y group by x,y having(y<10) order by y desc
    ==============================================================
    select:select y from table
    where:x=y
    group:x,y having(y<10)
    order:y desc