开发者

Find and Replace a section of a string with wildcard type search

开发者 https://www.devze.com 2022-12-29 03:20 出处:网络
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).

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.


content = Regex.Replace(content, @"M9(S\d{4})\.\d", "$1");
0

精彩评论

暂无评论...
验证码 换一张
取 消