如何在Regex中将此函数压缩为short?

时间:2009-09-22 02:05:08

标签: c# regex

    private string AccessToSQL(string accessString)

{
    accessString = Regex.Replace(accessString, "dbo_", String.Empty);
    accessString = Regex.Replace(accessString, "\"", "'");
    accessString = Regex.Replace(accessString, "%START%", startDate);
    accessString = Regex.Replace(accessString, "%STOP%", endDate);
    string midToSubs = Regex.Match(accessString, @"mid\(.+?\)", RegexOptions.IgnoreCase).Value;
    string fieldName = Regex.Replace(Regex.Match(midToSubs, @"\[.+?\]").Value, @"\[|\]", String.Empty);
    string toSubString = "SUBSTRING(" + fieldName + ", 2, LEN(" + fieldName + "))";
    accessString = Regex.Replace(accessString, @"mid\(.+?\)", toSubString, RegexOptions.IgnoreCase);
    return accessString;
}

必须Regex.Replace如何缩短?

1 个答案:

答案 0 :(得分:0)

您可以通过一次替换将MID转换为SUBSTRING

accessString = Regex.Replace(accessString,
                             @"mid\(.*?\[(.+?)\].*?\)",
                             "SUBSTRING($1, 2, LEN($1))",
                             RegexOptions.IgnoreCase);