【问题标题】:Powershell regex to replace quotes in capture group [duplicate]Powershell正则表达式替换捕获组中的引号[重复]
【发布时间】:2020-01-30 06:57:09
【问题描述】:

我正在使用这两行脚本来更新迁移脚本 - https://github.com/dotnet/efcore/issues/12911#issuecomment-505596919

$sql = get-content .\migration.sql -raw
[regex]::replace($sql, "BEGIN\s+(CREATE (?:VIEW|TRIGGER).+?)END", "BEGIN`nEXEC('`$1');`nEND", "ignorecase,singleline") > migration.sql

问题是它不能替换单引号,所以如果我有这样的查询:

BEGIN
CREATE VIEW someview AS
select lastname + ', ' + firstname from sometable
END;

我结束了

BEGIN
EXEC('CREATE VIEW someview AS select lastname + ', ' + firstname from sometable');
END;

那是行不通的。也许是因为这是一天的结束,我感觉很紧张,但我需要做些什么来替换它捕获的引号?我知道如何在 C# 中做到这一点,但我是 Powershell 新手。

【问题讨论】:

  • 正则表达式不匹配,因为存在select lastname 而不是CREATE VIEWCREATE TRIGGER。您要添加到正则表达式的规则是什么?
  • 我搞砸了我的复制/粘贴。立即修复。

标签: regex powershell


【解决方案1】:

试试这个:

$sql = get-content .\migration_old.sql -raw
[regex]::replace($sql, "BEGIN\s+(CREATE (?:VIEW|TRIGGER).+?)END", {param($match) "BEGIN`nEXEC('$($match.Groups[1].Value -replace "'", "''")');`nEND"}, "ignorecase,singleline") > migration_new.sql

【讨论】:

    猜你喜欢
    • 2014-08-30
    • 2010-11-19
    • 2013-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多