1

I have a program to compare text files. Takes in 2 files spits out 1. The input files have lines of data similar to this

tv_rocscores_DeDeP005M3TSub.csv FMR: 0.0009 FNMR: 0.023809524 SCORE: -4  Conformity: True
tv_..............P006............................................................
tv_..............P007............................................................

etc etc.

For my initial purposes, I was splitting the lines based on spaces, to get the respective values. However, for the first field, tv_rocscores_DeDeP005M3TSbu.csv i only need P005 and not the rest. I cannot opt for position number as well, because the position of P005 in the phrase is not the same for every file.

Any advise on how i split this so that i can identify my first field with only P005??

6
  • 1
    Is it always P005? What's the pattern here? Commented Jan 10, 2014 at 10:25
  • If you got a pattern as @germi was looking for you could extract the value via a regex. Commented Jan 10, 2014 at 10:28
  • That's just one line. I have from P001 to P100 in my text files. This was just for an example. Commented Jan 10, 2014 at 10:29
  • Please let me edit the question for better understanding Commented Jan 10, 2014 at 10:30
  • Without using regex: get a new line, find first occurrence of .csv, go back until you read Pxxx, take xxx. Should not be hard. Commented Jan 10, 2014 at 10:58

5 Answers 5

2

Your question is a bit unclear. If you're looking for pattern, say "P + three digits", e.g. "P005" you can use regular expressions:

  String str = @"tv_rocscores_DeDeP005M3TSub.csv FMR: 0.0009 FNMR: 0.023809524 SCORE: -4  Conformity: True";

  String[] parts = str.Split(' ');
  parts[0] = Regex.Match(parts[0], @"P\d\d\d").Value; // <- "P005"
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer @Dmitry. Lemme test that out please. :)
This worked like a charm for me. This is actually easier for me to use in my program. And so i am accepting this answer. Thank you very much again
1

To extract the desired part I would try something like this:

 var parts = str.Split(' ');
 var number = Regex.Match(parts[0], ".*?(?<num>P\d+).*?").Groups["num"].Value;

Or if you know its only three digits you could change the regular expression to .*?(?<num>P\d{3}).*?

Hope that solves your problem :)

1 Comment

Hi, Thanks for the answer. I already got an answer(same as yours). This goes well with my application. Thanks for taking time to answer my question.
0

How about just checking if the first field contains P005?

bool hasP005 = field1.Contains("P005");

1 Comment

Thanks for taking time to answer my question. I got my answer. Have a great day.
0

Your question isn't clear. Can't you just replace the first field with your string?

string[] parts = str.Split(' ');
parts[0] = "P005";

2 Comments

I am sorry my question wasn't clear. I have a program to compare two text files and subtract the respective fields (based on the first field tv_roc....P005...)
Thanks for taking time to answer my question
0

Are you looking to try field the field that contains that string? if so then you can use some linq

var field = s.Split(' ').Where(x => x.Contains("P005")).ToList()[0];

1 Comment

No worries. Thank you very much for taking time to answer my question. I got my answer. Thanks again

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.