2013-10-01 18 views
1

我的代码。替换x和x + 1之间出现的字符串的一部分c#

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. 
+1

_“//然后我将删除第一个和最后一个逗号”_这是这部分还是您的下一个问题? –

+0

你真的从中得到了什么结果?乍一看,我不认为你可以用这种方式使用正则表达式(虽然我的知识是有限的) – Nunners

+0

@Tim我提到,因为SQL语法 – Misi

回答

1

这样使用;

string sql = "INSERT INTO TABLE1(col1, col2, col3, col4, col5) VALUES (,NULL, NULL, NULL, NULL, NULL,)"; 

string nulls = "NULL"; 
List<int> indexes = new List<int>(); 
foreach (Match match in Regex.Matches(sql, nulls)) 
{ 
    indexes.Add(match.Index); 
} 

sql = sql.Remove(indexes[2], 4).Insert(indexes[2], "'abc'"); 
Console.WriteLine(sql); 

输出将是;

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

作为解释,这将在您的查询找到第三NULL,然后用'abc'替换本身。

这里a DEMO

+0

很好的例子,但是...当我想要时会发生什么做第二次插入?第一次插入后,索引列表不再对应。 – Misi

+0

@Misi你可以改变'sql = sql.Remove(indexes [2],4).Insert(indexes [2],''abc'“);'但是记住,当你插入第一个''abc' '索引可以从'0'到'3',因为你的SQL中有4个NULL,而不是5个。 –

1

您可以使用该扩展方法。 this的修改版本。

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); 
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 
0

我已经编辑从该VFP Toolkit for .NETStrExtract功能,并得到了,我叫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); 
相关问题