I have a file in which the necessary lines that need to be extracted are stored, the following code works to extract one part of the record:

string TLine = File.ReadAllLines(Text.df)[3].Split('"')[1]; 

Then I just write the found lines to the file.

And I also need to read the remaining lines of ReadAllLines(Text.df)[11].Split('"')[1]

They can be combined somehow so that you can read all the data from the desired column [3].Split('"')[1] и [11].Split('"')[7]

How can I read all the data I need from the right post?

Here is what is stored in the file:

 "Logins" { "895298918746" { "Name" "UserName" "City" "Moskow" "Date" "12/10/18" "Num" "14" "Timer" "99999999" } "895283716281" { "Name" "UserName" "City" "Moskow" "Date" "12/10/18" "Num" "14" "Timer" "99999999" } } 

I need to get the value 895298918746 and the UserName value from the Name field

Etc. 895283716281 and UserName value from the Name field

  • You would attach a file format and at least one line. Well, in general, the appeal on the index, I personally consider a crutch ... - EvgeniyZ
  • Well, you will File.ReadAllLines(Text.df) result File.ReadAllLines(Text.df) into a variable and then access it. What is the problem? - Andrei NOP
  • Well, from this and had to start. This is JSON and it is obviously not necessary to work with it through Split. - EvgeniyZ
  • Possible duplicate question: How to parse json into an associative array / C # - EvgeniyZ
  • I gave you the answer. You deserialize into a Dictionary and the key is your "random" value. - EvgeniyZ

1 answer 1

Friday evening, it's time for the regulars ...

 var data = File.ReadAllText("test.txt"); var pattern = @" ^ \s* "" (?'login'\d+) "" \s* ^ \s* { \s* ^ \s* ""Name"" \s* ""(?'Name' .*? )"" \s* ^ \s* ""City"" \s* ""(?'City' .*? )"" \s* ^ \s* ""Date"" \s* ""(?'Date' .*? )"" \s* ^ \s* ""Num"" \s* ""(?'Num' .*? )"" \s* ^ \s* ""Timer"" \s* ""(?'Timer' .*? )"" \s* ^ \s* } "; var options = RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline; var matches = Regex.Matches(data, pattern, options); var users = new List<User>(); foreach (Match m in matches) { var user = new User { Login = m.Groups["login"].Value, Name = m.Groups["Name"].Value, City = m.Groups["City"].Value, Date = m.Groups["Date"].Value, Num = int.Parse(m.Groups["Num"].Value), Timer = int.Parse(m.Groups["Timer"].Value) }; users.Add(user); } 

Class model for storing user information:

 public class User { public string Login { get; set; } public string Name { get; set; } public string City { get; set; } public string Date { get; set; } public int Num { get; set; } public int Timer { get; set; } } 
  • And by passing regular expressions how else can you? - Luser
  • Substring - Using String class methods: Substring , Contains , StartsWith , Split and others. - Alexander Petrov
  • Well this is understandable) There would be more similar examples. So I do not really shy how to combine all this - Luser