Created
October 7, 2014 22:57
-
-
Save ronnieoverby/f49095bbbd43c8a392cf to your computer and use it in GitHub Desktop.
file processing with coretechs.common
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
string text = @" | |
FRONT MATTER | |
FRONT MATTER #2 | |
NAME: Billy B Goode | |
BIRTHDATE: 5/10/1984 | |
GENDER: Male | |
MIDDLE MATTER | |
MIDDLE MATTER #2 | |
NAME: Martha Peppers | |
BIRTHDATE: 3/16/1984 | |
GENDER: Female | |
NAME: Smarty McSmarty | |
BIRTHDATE: 10/27/2005 | |
GENDER: Male | |
REAR MATTER"; | |
// regex that matches the key | |
// and captures the value | |
var rgx = new Regex(@"^[A-Z]+\: (.)"); | |
// get a sequence that has only lines I know will be used | |
var interestingLines = | |
from l in text.ReadLines() | |
where rgx.IsMatch(l) | |
select l; | |
// accumulate chunks of lines to process | |
interestingLines | |
.AccumulateUntil(x => x.StartsWith("NAME: ")) | |
.Select (x => new | |
{ | |
Name = rgx.Replace(x[0], "$1"), | |
BirthDate = rgx.Replace(x[1], "$1").Parse(DateTime.Parse), | |
Gender = rgx.Replace(x[2], "$1"), | |
}).Dump(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment