c# - How can I efficiently process a delimited text file? -


I just file against a specific file. Try reading all Linux , and for every line, | . I have to use regex on this one

This code does not work below, but you will see what I'm trying to do:

  string [] Content = file. Read all files (filename); String [] splitlines = Regex.Split (content, '|'); Fresh (string split in split lines) {// Regex line = content Split ('|'); //content.Split ('|'); String prefix = prefix = regesx. Match (line, @ "(\ S +) (\ d +)"). Group [0]. Guails; File. Appendical text (Working format 2 + "configuration.txt", prefix + "\ r \ n"); }  

It is not completely clear what you are trying to do , But there are many errors in your code. I have guessed what you are doing, but if this is not what you want, then please tell what you prefer preferably with some examples:

  String inputfile = "input" txt; String outputfile = "output.txt"; ({StreamReader streamReader = File.OpenText (inputFilename)) using {StreamWriter streamWriter = File.AppendText (outputFilename)} {while (true) {string line = streamReader.ReadLine (); If (line == zero) {break; } String [] splitlines = line.Split ('|'); Forchach (string split in split lines) {match match = reggae. Match (Partition, @ "\ S + \ d +"); If (match.Success) {string prefix = match.Groups [0] value; StreamWriter.WriteLine (prefix); } Else {// handle match fail ...}}}}}  

main point:

  • You want to perform an operation on each line , So you have to be repeated on lines.
  • Use simple string. Sample method if you want to split on a single character Regex.Split does not accept a character and "|" Regular expression has a special meaning, so it can not work unless you survive it.
  • You were opening the output file several times and were closing you should open it only once and should open it until you finish typing it.
  • If the input file is large, readAllLines instead of streamreader .
  • If the match fails, then your program will throw an exception. You should check the match.Success before using the match and if this is incorrect, then Handle the mistake correctly (leave the line, report an alert, make an exception with the appropriate message, etc.)
  • You are not actually using Groups 1 and 2 in regular expressions , So that you regularly express results by storing results Central to the engine by removing the brackets to save that you will not use anyway.

Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -