我有一个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]不是。我肯定错过了什么。有什么想法吗?
答案 0 :(得分:1)
您的代码适合我。我能想到的唯一问题是源代码中FROM
和[DOCUMENT]
之间的空格不止一个,在这种情况下,您可以删除空格或使用
FROM\\s+(?<table>\\S+)