捕获组的一部分

时间:2014-07-15 16:52:35

标签: c# regex

我想只捕获字符串中的数字 - $ 235,993.84。因此,当我访问我的组时,它将返回23599384。

\$(?<amount>\d*,\d{3}\.\d{2})

3 个答案:

答案 0 :(得分:2)

使用此:

Regex.Replace(foo, "[^0-9]", "");

答案 1 :(得分:1)

您可以使用Regex.Replace并使用String.Empty替换非数字,而不是进行捕获:

  string input = " $235,993.84";
  string replacement = "";
  Regex rgx = new Regex("[^0-9]");
  string result = rgx.Replace(input, replacement);

  // result now contains 23599384

答案 2 :(得分:1)

对于这个例子,你不应该需要Regex:

string test = "$235,993.84";
string onlyNums = new string(test.Where(c => char.IsDigit(c)).ToArray());
相关问题