【C#、Regex】像 #if DEBUG and #endif 删除指定内容

最近想发布 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 ^__^


关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章