[英]Multiline search and replace using C#/class Regex
I've some stored procedures that contain stuff like this: 我有一些包含以下内容的存储过程:
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.
As said, there are procedures, so many instructions are in the same script. 如前所述,有一些过程,所以同一脚本中有很多指令。
I load each of theses procedures in a StringBuilder (that contains the same script that shown above). 我将这些过程中的每个过程都加载到StringBuilder中 (它包含与上面显示的脚本相同的脚本)。
I want to remove the HAVING part if (and only IF
!). 我想删除HAVING部分,如果(并且只有IF
!)。 It is exactly the same as in the WHERE part (as above). 它与WHERE部分完全相同(如上所述)。
So I immediatly thought at the regular expressions. 因此,我立即想到了正则表达式。
I've something like this: 我有这样的事情:
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("---");
}
It works, but it does not seem to be the best way... 它有效,但似乎不是最好的方法...
How do I detect safely the end of the HAVING? 如何安全检测HAVING的结束?
How would you manage this work? 您将如何管理这项工作?
First thought is this: 首先想到的是:
string pattern = @"WHERE\s+([\s\S]*?)\s+HAVING\s+\1\s+(SELECT|$)";
string output = Regex.Replace(input, pattern, @"WHERE $1 SELECT");
However, this will only work if the statement is immediately followed by the SELECT keyword or an end-of-line. 但是,这仅在语句后紧跟SELECT关键字或行尾的情况下有效。 Different use of whitespace in the conditionals will also throw it off, as will reordering of subclauses. 条件子句中对空格的不同使用也会使它失去作用,子句的重新排序也一样。 If you want something that's going to do this in a robust way, it's going to be VERY complicated without some kind of specialized SQL parser/optimizer. 如果您想要以健壮的方式执行此操作,那么如果没有某种专门的SQL解析器/优化器,它将变得非常复杂。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.