日期:2014-05-17  浏览次数:20413 次

正则查找包含某个字符串
string viewScript = " left join (select T1 as T2 from Table group by T1 ) as t on t (select 1 from TT) as c";

我想得到的结果是 left join (select T1 as T2 from Table group by T1 ) t on t (select 1 from TT) c

我是样写只能替换掉最后一个as
viewScript = Regex.Replace(viewScript, "join(.*)as ", "join $1", RegexOptions.IgnoreCase);

正则里怎么加个from,但不知道写
viewScript = Regex.Replace(viewScript, "join((from).*)as ", "join $1", RegexOptions.IgnoreCase);

------解决方案--------------------
C# code

string viewScript = " left join (select T1 as T2 from Table group by T1 ) as t on t (select 1 from TT) as c";
viewScript = Regex.Replace(viewScript, @"(?i)as ", "");

------解决方案--------------------
C# code

string viewScript = " left join (select T1 as T2 from Table group by T1 ) as t on t (select 1 from TT) as c";

                viewScript = Regex.Replace(viewScript,@"(?<=join\s*?\([^)]+?\).*?)\s+as\s+"," ");
                // left join (select T1 as T2 from Table group by T1 ) t on t (select 1 from TT) c

------解决方案--------------------
C# code

            string source = "left join (select T1 as T2 from Table group by T1 ) as t on t (select 1 from TT) as c";
            Regex reg = new Regex(@"(?<=\([^\)]*?\) )as");
            source = reg.Replace(source, @"");

------解决方案--------------------
C# code

 string sql=@"string viewScript = "" left join (select T1 as T2 from Table group by T1 ) as t on t (select 1 from TT) as c";
 sql= Regex.Replace(sql, @"(?<=\)\s+)as", "");