代码之家  ›  专栏  ›  技术社区  ›  MicMit

.Net正则表达式替换-在每个单词前面加上特定字符

  •  0
  • MicMit  · 技术社区  · 14 年前

    输入“col1,col2,col3,coln”

    输出“@col1,@col2,@col3,@coln”

    2 回复  |  直到 14 年前
        1
  •  5
  •   Ahmad Mageed    14 年前

    string input = "col1, col2, col3, coln";
    string pattern = @"\b(\w+)\b";
    string result = Regex.Replace(input, pattern, "@$1");
    Console.WriteLine(result);
    
        2
  •  1
  •   Dean Harding    14 年前
    string input = "col1, col2, col3, coln";
    string result = "@" + Regex.Replace(input, @"\s+", " @");
    

    当然,这取决于你希望它有多强大。但是,如果它只是针对SQL查询参数(看起来就是这样),那么上面的内容就可以了。