I'm trying to turn this string:
INSERT INTO [main].[sqlite_default_schema].[TableName] ([SomeColumn], [SomeOtherColumn], [Last]) VALUES (@param1, @param2, @param3);
into this one:
INSERT INTO [main].[sqlite_default_schema].[TableName] ([SomeColumn], [SomeOtherColumn], [Last]) VALUES (@SomeColumn, @SomeOtherColumn, @Last);
As a beginner in regex, i'm using this C# snippet:
Regex regex = new Regex(@"\(.*?\)");
MatchCollection matches = regex.Matches(commandText);
if (matches[0].Success && matches[1].Success)
{
Regex reColNames = new Regex(@"\[\w*\]");
MatchCollection colNames = reColNames.Matches(matches[0].Value);
Regex reParamNames = new Regex(@"\@\w*");
MatchCollection paramNames = reParamNames.Matches(matches[1].Value);
if (colNames.Count > 0 && colNames.Count == paramNames.Count)
{
for (int i = 0; i < paramNames.Count; i++)
{
string colName = colNames[i].Value.Substring(1, colNames[i].Length - 2);
commandText = commandText.Replace(paramNames[i].Value, "@" + colName);
}
}
}
return commandText;
This works but doesn't feel right. Is there a way to achieve the same result by using just one regex?
Cheers!