Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 解析SQL Insert语句并获取N'VALUE'之间的所有字符串值_C#_Regex_Str Replace_Regexp Replace - Fatal编程技术网

C# 解析SQL Insert语句并获取N'VALUE'之间的所有字符串值

C# 解析SQL Insert语句并获取N'VALUE'之间的所有字符串值,c#,regex,str-replace,regexp-replace,C#,Regex,Str Replace,Regexp Replace,我有一个大型SQL Server插入脚本 INSERT [dbo].[Table] ([Key], [Name], [Value], [Module]) VALUES (1, N'Product', N'Value 1', N'Value 2') 现在我需要逐行解析这个大文件,对于NVARCHAR N'VALUEHERE'的每个值,我想用从数据库中获取的一些自定义信息替换它 如果这是我的脚本: INSERT [dbo].[Table] ([Key], [Name], [Value], [Mo

我有一个大型SQL Server插入脚本

INSERT [dbo].[Table] ([Key], [Name], [Value], [Module]) 
VALUES (1, N'Product', N'Value 1', N'Value 2')
现在我需要逐行解析这个大文件,对于NVARCHAR N'VALUEHERE'的每个值,我想用从数据库中获取的一些自定义信息替换它

如果这是我的脚本:

INSERT [dbo].[Table] ([Key], [Name], [Value], [Module]) 
VALUES (1, N'Product', N'Value 1', N'Value 2')
解析后,我想将其更新为:

INSERT [dbo].[Table] ([Key], [Name], [Value], [Module]) 
VALUES (1, N'Product Changed', N'Value 1 with some custom info', N'Value 2 with different info')
当然,实际情况有点不同

在big中,我想获取所有nvarchar值,对于以N'开头的每个值,从单引号中获取该值,对其执行一些操作并用新值更新它,然后移动到下一个N'等等

我的问题:

如何获取引号N'value'之间的每个值更改该值并替换为新值N'CHANGED'

还有一个问题,如果值类似于:

 `N'Jimm''s device is broken'`

我假设您想对单引号之间的值进行一些逻辑处理。如果您只需要一个简单的字符串替换,那么正则表达式替换会更好

您可以使用正则表达式匹配及其捕获组来选择正则表达式匹配的片段。然后,您可以使用捕获组字符串索引值来操作原始字符串

string input = @"INSERT [dbo].[Table] ([Key], [Name], [Value], [Module]) VALUES (1, N'Product', N'Value 1', N'Value 2')";

Regex regex = new Regex("N'([^']+)'");
MatchCollection matches = regex.Matches(input);

//Loop though matches backwards so the index values don't get misaligned
for (int i = matches.Count - 1; i >= 0; i--)
{
    //Get the capture group info for the content between the single quotes
    Group captureGroup = matches[i].Groups[1];

    //Replace the contents of the input string with some updated value
    input = input.Substring(0, captureGroup.Index) + SomeStringMethod(captureGroup.Value) + input.Substring(captureGroup.Index + captureGroup.Length);
}

Console.WriteLine(input);

之后只需更新数据库。正如我提到的场景有点不同,信息来自rest api,因此我需要使用C。当然这将非常好,但我没有直接访问db的权限,我将从那里获取信息。问题是如何更新文本文件并替换值,或者它是否涉及将数据返回数据库?@Noel我需要在此时更新SQL脚本我删除了SQL标记,因为这与SQL无关。它是关于转换文本文件的。
string input = @"INSERT [dbo].[Table] ([Key], [Name], [Value], [Module]) VALUES (1, N'Product', N'Value 1', N'Value 2')";

Regex regex = new Regex("N'([^']+)'");
MatchCollection matches = regex.Matches(input);

//Loop though matches backwards so the index values don't get misaligned
for (int i = matches.Count - 1; i >= 0; i--)
{
    //Get the capture group info for the content between the single quotes
    Group captureGroup = matches[i].Groups[1];

    //Replace the contents of the input string with some updated value
    input = input.Substring(0, captureGroup.Index) + SomeStringMethod(captureGroup.Value) + input.Substring(captureGroup.Index + captureGroup.Length);
}

Console.WriteLine(input);