最近想发布 SQL Script 档案到正式环境时,自动将 Dev 测试脚本部分移除
尝试卡住发问在S.O得到 Nick 解答 c# - How like '#if DEBUG' and '#endif' remove content between two marks - Stack Overflow
举例:
有一个 A.sql 内容:
select id ,name /**if DEBUG**/ ,test1 ,test2 /**endif**/from tablewhere sDate >= '2021-01-01' /**if DEBUG**/ and test1 = '123456' and test2 = '123456' /**endif**/
想变成
select id ,namefrom tablewhere sDate >= '2021-01-01'
可以这样做
void Main(){var input = @"select id ,name /**if DEBUG**/ ,test1 ,test2 /**endif**/from tablewhere sDate >= '2021-01-01' /**if DEBUG**/ and test1 = '123456'and test2 = '123456' /**endif**/ ";var output = Regex.Replace(input, @"/\*\*if\sDEBUG\*\*/.*?/\*\*endif\*\*/", "", RegexOptions.Singleline);Console.WriteLine(output);}
注意:特别使用 RegexOptions.Singleline
因为它将整个输入字符串视为一行,忽略了换行符。
觉得有帮助可以帮 Nick 点个 Upvote ^__^