0

私のコード。

string sql = "INSERT INTO TABLE1(col1, col2, col3, col4, col5) VALUES (, NULL, NULL, NULL, NULL, NULL,)";
//Insert value for the third column(between the 3rd and 4th comma)
Regex rgx = new Regex(@", null"); 
sql = rgx.Replace(sql, ", 'abc'", 3);// this doesn't work
sql = rgx.Replace(sql, ", 'def'", 4);// second insert

望ましい結果

sql = "INSERT INTO TABLE1(col1, col2, col3, col4, col5) VALUES (, NULL, NULL, 'abc', 'def', NULL,)";
//Then I'll remove the first and last comma between the VALUES parenthesis.
4

6 に答える 6

1

この拡張メソッドを使用できます。これの修正版。

public static string ReplaceSpecifiedIndex(this string input, string valueToBeReplaced, string replacingvalue, int index)
  {
            input = input.ToLower();
            valueToBeReplaced = valueToBeReplaced.ToLower();
            replacingvalue = replacingvalue.ToLower();
            Match m = Regex.Match(input, "((" + valueToBeReplaced + ").*?){" + index + "}");
            int specificIndex = -1;
            if (m.Success)
                specificIndex = m.Groups[2].Captures[index - 1].Index;

     if (specificIndex > -1)
     {
                string temp = input.Substring(specificIndex, valueToBeReplaced.Length);
                int nextsubstring = specificIndex + valueToBeReplaced.Length;
                input = input.Substring(0, specificIndex) + temp.Replace(valueToBeReplaced, replacingvalue) + input.Substring(nextsubstring);
      }
      return input;
  }

そしてそれをこのように呼びます

string sql = "INSERT INTO TABLE1(col1, col2, col3, col4, col5) VALUES (, NULL, NULL, NULL, NULL, NULL,)";
sql = sql.ReplaceSpecifiedIndex("null", "abc", 3);
于 2013-10-01T07:50:33.303 に答える
1

正規表現なし:

string sql = "INSERT INTO TABLE1(col1, col2, col3, col4, col5) VALUES (, NULL, NULL, NULL, NULL, NULL,)";
int indexOfvalues = sql.IndexOf("VALUES (");
if (indexOfvalues >= 0)
{
    indexOfvalues += "VALUES (".Length;
    int endIndexOfvalues = sql.IndexOf(")", indexOfvalues);
    if (endIndexOfvalues >= 0)
    {
        string sqlValues = sql.Substring(indexOfvalues, endIndexOfvalues - indexOfvalues);
        string[] values = sqlValues.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
        if(values.Length >= 3)
            values[2] = "'abc'";
        string newValues = string.Join(",", values);
        sql = string.Format("{0}{1})", sql.Substring(0, indexOfvalues), newValues.Trim());
    }
}

結果:

INSERT INTO TABLE1(col1, col2, col3, col4, col5) VALUES (NULL, NULL, 'abc', NULL, NULL)

または、String.Split短くて読みやすい(おそらくもう少し危険です):

string sql = "INSERT INTO TABLE1(col1, col2, col3, col4, col5) VALUES (, NULL, NULL, NULL, NULL, NULL,)";
string[] tokens = sql.Split(new[] { "VALUES" }, StringSplitOptions.None);
if (tokens.Length == 2)
{
    string[] values = tokens[1].Trim('(', ')', ',', ' ').Split(',');
    if (values.Length >= 3)
        values[2] = "'abc'";
    string newValues = string.Join(",", values);
    sql = string.Format("{0} VALUES ({1})", tokens[0], newValues);
}

// same result
于 2013-10-01T07:51:02.217 に答える
0

VFP Toolkit for .NETStrExtractの関数を編集し、 .NETという名前の新しい関数を取得しました。StrReplace

public static string StrReplace(string cSearchExpression, string replacement, string cBeginDelim, string cEndDelim, int nBeginOccurence)
    {
        string cstring = cSearchExpression;
        string cb = cBeginDelim;
        string ce = cEndDelim;

        if (cSearchExpression.Contains(cBeginDelim) == false && cSearchExpression.Contains(cEndDelim) == false)
        {
            return cstring;
        }

        //Lookup the position in the string
        int nbpos = At(cb, cstring, nBeginOccurence) + cb.Length - 1;
        int nepos = cstring.IndexOf(ce, nbpos + 1);

        //Reaplce the part of the string if we get it right
        if (nepos > nbpos)
        {
            cstring = cstring.Remove(nbpos, nepos - nbpos).Insert(nbpos, replacement);
        }
        return cstring;
    }

At関数はツールキットにあります。

sql = strings.StrReplace(sql , " abc", ",", ",", 3);
sql = strings.StrReplace(sql , " def", ",", ",", 4);
于 2013-10-01T12:23:27.680 に答える