1

我有一个作为字符串的 SQL 查询,我正在尝试使用正则表达式对其进行查找和替换。

var s = "SELECT ([Document].[ArtifactID]) FROM [Document]
        (NOLOCK) WHERE [Document].[AccessControlListID_D] 
        IN (1) AND ((NOT EXISTS(SELECT CodeArtifactID 
        FROM CodeArtifact (NOLOCK) WHERE AssociatedArtifactID = 
        [Document].[ArtifactID] AND CodeArtifact.CodeTypeID = 1000112))) ";
//line breaks added for readability

为了使查询工作,我需要指定 [Document] 和 CodeArtifact 的架构。我有:

var pattern = "FROM\\s(?<table>\\S+)";
var replacePattern = "FROM EDDSDBO.${table}";

var v = Regex.Replace(s, pattern, replacePattern);
System.Console.WriteLine(v);

但我回来的字符串看起来像:

SELECT ([Document].[ArtifactID]) 
FROM [Document] (NOLOCK) 
WHERE [Document].[AccessControlListID_D] IN (1) AND 
((NOT EXISTS(SELECT CodeArtifactID 
             FROM EDDSDBO.CodeArtifact (NOLOCK) 
             WHERE AssociatedArtifactID = [Document].[ArtifactID] AND CodeArtifact.CodeTypeID = 1000112))) 

CodeArtifact 已被替换,但 [Document] 未被替换。我肯定错过了什么。有什么想法吗?

4

1 回答 1

1

你的代码对我有用。FROM我能想到的唯一问题是源代码之间和之间有多个空格[DOCUMENT],在这种情况下,您可以删除空格或使用

FROM\\s+(?<table>\\S+)
于 2013-08-23T23:12:24.940 回答