tags:

views:

92

answers:

1
    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;
}

have must Regex.Replace how to shorter?

A: 

You could convert MID to SUBSTRING with a single replacement:

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