Regular expression replace in C#

前端 未结 3 1785
清酒与你
清酒与你 2020-12-30 18:59

I\'m fairly new to using regular expressions, and, based on a few tutorials I\'ve read, I\'m unable to get this step in my Regex.Replace formatted properly.

Here\'s

3条回答
  •  有刺的猬
    2020-12-30 19:39

    You can do it this with two replace's

    //let stw be "John Smith $100,000.00 M"
    
    sb_trim = Regex.Replace(stw, @"\s+\$|\s+(?=\w+$)", ",");
    //sb_trim becomes "John Smith,100,000.00,M"
    
    sb_trim = Regex.Replace(sb_trim, @"(?<=\d),(?=\d)|[.]0+(?=,)", "");
    //sb_trim becomes "John Smith,100000,M"
    
    sw.WriteLine(sb_trim);
    

提交回复
热议问题