Thursday, January 24, 2013

CacheRepository Use Case - Reading From (Somewhat) Unstructured Text File

Yesterday I was given a text file with a rather weird format that contained several hundred lines like this.



I needed to pull out the first sequence of numbers after the "work place name". I was able to use CacheRepository for this quite easily. I created an entity with the same name as the text file. Then it was just a matter of parsing a string (line) to get out the data I wanted.

public class ActiveEmployer
{
public ActiveEmployer(string line)
{
this.Line = line;
if (line.Contains("==")) return;
var match = new Regex(@" \d\d\d\d\d? ").Match(line);
this.Identification = match.Value.Trim();
}
public string Identification { get; set; }
public string Line { get; set; }
}
var configBuilder = new FileRepositoryConfigBuilder(".")
.WithFileExtension(".txt")
.Build();
using (var fileRepository = configBuilder.BuildRepository())
{
var activeEmployers = fileRepository.GetAll<ActiveEmployer>();
foreach (var activeEmployer in activeEmployers)
{
if (activeEmployer.Identification == null) continue;
// Do some stuff with the activeEmployer.Identification
}
}
view raw readFromFile.cs hosted with ❤ by GitHub

No comments:

Post a Comment