想用正则表达式来分析语法,实现一个小应用,如 下边我以分析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
我对正则不是很懂,经常晕的,嵌套我暂时先不举例,等先把上边的分析出来了再说,先谢谢大家
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
我对正则不是很懂,经常晕的,嵌套我暂时先不举例,等先把上边的分析出来了再说,先谢谢大家
解决方案 »
- 求一个c# 的邮件管理类库
- there is a problem with this windows installer package
- 进程创建的线程,当进程被强制关闭,线程会结束么?
- 第三方生产的ThinkPad笔记本外置光驱(DVD刻录机)价格?
- 将一个程序改为相关程序该如何下手?
- 请问如何用Type(反射)来读取某一控件(以TextBox为例)的所有的属性名称???
- 精疲力尽!求救! 关于ASP.net C#调用VB的ActiveX DLL
- 晕死,在窗口最大最小化还原都没问题,窗口上方按右键最大化最小化还原都行。但在任务栏上右键就有时行有时不行。
- pro.StandardInput.WriteLine();怎么用
- 请高手推荐有关C#的DirectX编程方面的书或资料
- button响应事件问题
- VS2010和sliverlight的安装
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