C#Regex替换所有以特定字符开头的单词

时间:2017-05-21 19:16:26

标签: c# regex

我有一个字符串,我想替换所有以$开头并以(空格)结尾的单词。

这是最好的做法吗?

1 个答案:

答案 0 :(得分:0)

我建议从明显的What is a common way to save 'debit' and 'credit' information?开始;例如让我们把这些单词改成大写:

  string source = "$word $another skip $final preserve $end";

  string result = Regex.Replace(
      source,    // scan source
    @"\$\w+\b",  // starts with $, contains words symbols
      match => match.Value.ToUpper()); // turn into upper case 

  Console.Write(result);

结果:

 $WORD $ANOTHER skip $FINAL preserve $END