user2704319
user2704319

Reputation: 33

How can I extract specific strings from a text file and add to List?

This is the code I am using now:

private void words(string path)
{
    List<string> text = new List<string>();
    var intro = "Video File Name:";
    var words = File.ReadAllLines(path)
        .Where(line => line.StartsWith(intro))
        .Select(line => line.Substring(intro.Length).Trim());   
}

The variable path is a link of text file. This is the content format of the text file:

Video File Name: MVI_4523.MOV
 
Lightning Start Location At Frame: 11     Lightning End Location At Frame: 15
Lightning Start Location At Frame: 6     Lightning End Location At Frame: 15
 
Video File Name: MVI_4524.MOV
 
Lightning Start Location At Frame: 15     Lightning End Location At Frame: 19
Lightning Start Location At Frame: 4     Lightning End Location At Frame: 19

What i want to do is to parse all video files names from the text file to a List<string> for example the List content will be:

index[0] MVI_4523.MOV

index[1] MVI_4524.MOV

Then I want to loop over the List and compare each index to a variable I have: like string variable = videoFile

For example:

for (int i = 0; i < videosfilesnames.Length; i++)
{
    if (videosfilesnames[i] == videoFile)
    {
        // Here I want to extract from the text file the lightnings values that are belong to the matched video file name for example if MVI_4524.MOV was == to videoFile so here i want to extract the values from the text file: 15 19 and 4 19
        // Then in the end i need to create/draw point red point on the locations 15 to 19 and 4 to 19
        //  So when extracting the values I need to be able to know that 15 is start and 19 end.
    }
}

Upvotes: 0

Views: 1054

Answers (1)

James
James

Reputation: 82136

It's not just the video names you need to extract here then, it's all the data e.g.

public class Lightening
{
    public Lightening(int start, int end)
    {
        StartLocation = start;
        EndLocation = end;
    }

    public int StartLocation { get; private set; }

    public int EndLocation { get; private set; }
}

public class Video
{
    public Video(string name)
    {
        Name = name;
        Lightenings = new List<Lightening>();
    }

    public string Name { get; private set; }

    public List<Lightening> Lightenings { get; private set; }
}
....
private List<Video> ExtractInfo(string path)
{
    var videos = new List<Video>();
    Video currentVideo = null;
    using (var file = new System.IO.StreamReader(path))
    {
        string line;
        Regex regex = new Regex(@"\d+");
        while((line = file.ReadLine()) != null)
        {
            if (line.StartsWith("Video"))
            {
                currentVideo = new Video(line.Split(':')[1].Trim());
                videos.Add(currentVideo);
            }
            else if (line.StartsWith("Lightning"))
            {
                var matches = regex.Matches(line);
                if (matches.Count == 2 && currentVideo != null)
                {
                    var l = new Lightening(Int32.Parse(matches[0].Value), Int32.Parse(matches[1].Value));
                    currentVideo.Lightenings.Add(l);
                }
            }
        }
    }
    return videos;
}

This gives you all the information you need for the rendering

for (int i = 0; i < videos.Count; i++)
{
    if (videos[i].Name == videoFile)
    {
        foreach (var l in videos[i].Lightenings)
        {
            // draw from l.StartLocation to l.EndLocation
        }
    }
}

Upvotes: 1

Related Questions