Regex to remove all (non numeric OR period)

后端 未结 5 885
陌清茗
陌清茗 2021-02-02 04:46

I need for text like \"joe ($3,004.50)\" to be filtered down to 3004.50 but am terrible at regex and can\'t find a suitable solution. So only numbers and periods should stay -

5条回答
  •  盖世英雄少女心
    2021-02-02 05:18

    For the accepted answer, MatthewGunn raises a valid point in that all digits, commas, and periods in the entire string will be condensed together. This will avoid that:

    string s = "joe.smith ($3,004.50)";
    Regex r = new Regex(@"(?:^|[^w.,])(\d[\d,.]+)(?=\W|$)/)");
    Match m = r.match(s);
    string v = null;
    if (m.Success) {
      v = m.Groups[1].Value;
      v = Regex.Replace(v, ",", "");
    }
    

提交回复
热议问题