查找和替换有关 RegEx.Replace 的问题
我有一个文本文件,我希望能够将以下所有实例更改:
T1M6 为 N1T1M6
T 始终是不同的值取决于加载的文本文件。例如,有时可能是
T2M6
,需要将其转换为
N2T2M6。
N(值)必须匹配T(值)。 M6永远是M6。
另一个例子:
T9M6 将翻译为 N9T9M6
这是我加载文本文件的代码:
StreamReader reader = new StreamReader(fDialog.FileName.ToString());
string content = reader.ReadToEnd();
reader.Close();
这是我想出的 RegEx.Replace 语句。不确定是否正确。
content = Regex.Replace(content, @"(T([-\d.]))M6", "N1$1M6");
它似乎可以搜索 T5M6 并将其转换为 N1T5M6。
但我不确定如何将 N(值) 转换为 T 的值。例如N5T5M6。
有人可以告诉我如何修改我的代码来处理这个问题吗?
谢谢。
I have a text file and I want to be able to change all instances of:
T1M6 to N1T1M6
The T will always be a different value depending on the text file loaded. So example it could sometimes be
T2M6
and that would need to be turned into
N2T2M6.
The N(value) must match the T(value). The M6 will always be M6.
Another example:
T9M6 would translate to N9T9M6
Here is my code to do the loading of the text file:
StreamReader reader = new StreamReader(fDialog.FileName.ToString());
string content = reader.ReadToEnd();
reader.Close();
Here is RegEx.Replace statement that I came up with. Not sure if it is right.
content = Regex.Replace(content, @"(T([-\d.]))M6", "N1$1M6");
It seems to work at searching for T5M6 and turning it into N1T5M6.
But I am unsure how to turn the N(value) into the value that T is. For example N5T5M6.
Can someone please show me how to do modify my code to handle this?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
像这样:
此外,您可能应该将
[-\d.]
替换为\d
或-?\d\.?
Like this:
Also, you should probably replace
[-\d.]
with\d
or-?\d\.?