使用C#/ class Regex进行多行搜索和替换

时间:2011-04-12 18:04:34

标签: c# regex

我有一些包含这样的东西的存储过程:

SELECT columnA, columnB, COUNT(*) AS "COUNT" INTO temporaryTable
FROM tableA
WHERE columnA = "A"
  AND ISNULL(columnB, "B") = "B"
GROUP BY columnA, columnB
HAVING columnA = "A"
  AND ISNULL(columnB, "B") = "B"
SELECT * FROM temporaryTable -- There is not necessary to have an empty line between two instructions.

如上所述,有一些程序,因此许多指令都在同一个脚本中。

我在StringBuilder中加载了每个程序(包含上面显示的相同脚本)。

我想删除HAVING部分if(并且只有IF!)。它与WHERE部分完全相同(如上所述)。

所以我立刻想到了正则表达式。

我有这样的事情:

    static string RemoveHaving(Match m)
    {
        if (m.Groups[3].Value == m.Groups[7].Value)
        { /* WHERE == HAVING */
            Console.WriteLine("Same");
            return string.Concat(m.Groups[1].Value, m.Groups[9].Value);
        }

        Console.WriteLine("Not Same");
        return m.Groups[0].Value;
    }

    static void Main(string[] args)
    {
        // For the example:
        StringBuilder procedure = new StringBuilder();
        procedure.Append(@"
            SELECT columnA, columnB, COUNT(*) AS "COUNT" INTO temporaryTable
            FROM tableA
            WHERE columnA = "A"
              AND ISNULL(columnB, "B") = "B"
            GROUP BY columnA, columnB
            HAVING columnA = "A"
              AND ISNULL(columnB, "B") = "B"
            SELECT * FROM temporaryTable -- There is not necessary to have an empty line between two instructions.");

        Regex reg = new Regex(@"((.*)where(.*)([\s^]+)group\s*by(.*)([\s^]+))having(.*)([\s^]+(SELECT|INSERT|UPDATE|DELETE))",
            RegexOptions.Compiled |
            RegexOptions.IgnoreCase |
            RegexOptions.Multiline);

        string newProcedure = reg.Replace(procedure, (MatchEvaluator)RemoveHaving);
        Console.WriteLine("---");
        Console.WriteLine(newProcedure);
        Console.WriteLine("---");
    }

它有效,但它似乎不是最好的方式......

如何安全地检测到HAVING的结束?

你将如何管理这项工作?

1 个答案:

答案 0 :(得分:1)

首先想到的是:

string pattern = @"WHERE\s+([\s\S]*?)\s+HAVING\s+\1\s+(SELECT|$)";
string output = Regex.Replace(input, pattern, @"WHERE $1 SELECT");

但是,只有紧跟SELECT关键字或行尾后面的语句时,这才有效。在条件句中不同地使用空格也会将其抛弃,子条款的重新排序也是如此。如果你想要以强大的方式做到这一点,那么如果没有某种专门的SQL解析器/优化器,它将变得非常复杂。