【发布时间】:2014-08-27 09:12:48
【问题描述】:
我有一个需求,我需要从 access db 读取查询并修改查询以将查询中的所有关键字替换为 SQL 可执行查询。 例如:假设有一个查询“从 table1 中选择键”
这里的“key”是保留关键字。必须修改为“Select [key] from table1”(在sql中可以使用)。
查询可能有多个关键字..所以我有一个如下的正则表达式。
string pattern = "Key|IN|ON|VIEW]";
Regex rgx = new Regex(pattern);
我有下面的代码..它检查模式..
string pattern = "Key|IN|ON|VIEW]";
Regex rgx = new Regex(pattern);
string strModifiedQuery = string.Empty;
strModifiedQuery = strQueryText;
foreach (Match m in rgx.Matches(pattern))
{
Console.WriteLine(m.Value);
if (m.Value == "Key")
{
strModifiedQuery = rgx.Replace(strModifiedQuery, "[Key]");
}
if (m.Value == "IN")
{
strModifiedQuery = rgx.Replace(strModifiedQuery, "[IN]");
}
if (m.Value == "ON")
{
strModifiedQuery = rgx.Replace(strModifiedQuery, "[IN]");
}
在 c# 中是否有更好的方法将匹配的模式替换为适当的值?
【问题讨论】:
-
Key|IN|ON|VIEW]错误模式...