正则表达式替换文件中的数字

Yon*_*ing 5 c# regex

在文件中有很多像200 20.5 329.2 ...的数字.现在,我需要用A*0.8替换每个数字A. 是否有任何简单的方法可以根据原始值替换原始值?

最好的祝福,

Jen*_*ens 8

试试这个:

String s = "This is the number 2.5. And this is 7";
s = Regex.Replace(s, @"[+-]?\d+(\.\d*)?", m => {return (Double.Parse(m.ToString())*0.8).ToString();});
// s contains "This is the number 2. And this is 5.6"
Run Code Online (Sandbox Code Playgroud)

编辑:在前面添加加号/减号作为可选字符.为避免将3-5中的5作为负数,您可以使用((?<=\s)[+-])?而不是[+-]