捕获组的一部分

本文关键字:一部分 | 更新日期: 2023-09-27 18:13:15

我只想捕获像这样的字符串中的数字- $235,993.84。所以当我访问我的组时,它会返回23599384。

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

捕获组的一部分

使用

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

不进行捕获,您可以使用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

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

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