使用通配符类型搜索查找并替换字符串的一部分
我希望能够读取文件,并
用 S1800 替换 M9S1800.2 的所有实例(我想删除 M9 和 .2)。
我遇到的问题是“S”后面的 4 位数字每次都可能不同,小数点后的数字也是如此。
例如,
它可以是:M9S3200.1 或 M9S5400.1 或 M9S5400.2
有人可以告诉我如何使用 C# 执行此操作吗?
我知道如何使用此代码查找和替换:
StreamReader reader = new StreamReader(fDialog.FileName.ToString());
string content = reader.ReadToEnd();
reader.Close();
content = Regex.Replace(content, "M2", "M3");
但对于 M9S3200.1,我想进行通配符类型替换。例如,就像用 S* 替换 M9S*.1,这样 M9S3200.1 就会变成 S3200。
I want to be able to read a file, and replace all instances of:
M9S1800.2 with S1800 (I want to get rid of the M9 and the .2).
The problem I am having is that the 4 digit number after the "S" can be different every time, as well as the number after the decimal.
For example,
It can be: M9S3200.1 or M9S5400.1 or M9S5400.2
Can someone please show me how to do this with C#?
I know how to find and replace by using this code:
StreamReader reader = new StreamReader(fDialog.FileName.ToString());
string content = reader.ReadToEnd();
reader.Close();
content = Regex.Replace(content, "M2", "M3");
but with the M9S3200.1, I want to do a wildcard type replace. For example it would be like replace M9S*.1 with S* so M9S3200.1 would just become S3200.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)