1

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.

1
  • Really??? If you don't have the data on 'N' from the pathology report, what is the advantage in synthesizing values of N based on T? In the future, records will contain N but not to-date? How will you distinguish values of N which were measured and values of N which were calculated this way? It might be better to represent each of these in its own column, and output '-' for values that weren't recorded. If you need to re-output with "calculated N" values, then you can do so. May 13, 2010 at 22:18

1 Answer 1

3

Like this:

string content = File.ReadAllText(fDialog.FileName.ToString());
content = Regex.Replace(content, @"T([-\d.])M6", "N$1T$1M6");

Also, you should probably replace [-\d.] with \d or -?\d\.?

2
  • What does \d do that [-\d.] doesn't do? With the change, would it look like: @"T(\d)M6", "N$1T$1M6"); ?
    – fraXis
    May 13, 2010 at 22:22
  • \d only matches digits; I have no idea what you're trying to match. Yes, it would.
    – SLaks
    May 13, 2010 at 23:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.