Question

I am trying to write a RegEx to extract data from a file.

The file looks like the following:

"a123 100 Start"
"a123 101 Today"
"a123 101 Tomorrow"
"a123 102 End"

The file contains multiple lines of records just like the one above. In each line of the file there is a code on a fixed position (100 - start of record, 101 - record detail, 102 - end of record). I would like to extract from that file a structure like a List<List<string>> where the outer list will store all the groups of records that are in the file.

My first approach was to parse this file with a foreach but I think there should be a way to achieve this with a RegEx. And being that I would like to expand my RegEx knowledge, I think that is a great example for me.

Is it possible for such data to be parsed with a RegEx? If so, can someone help out with the RegEx itself?

Thanks!

Was it helpful?

Solution

If your file has this specific structure, you do not need to use Regex. Just use Split(" ") and the result array for each line.

Regex has performance penalties.

But if you like to use Regex anyway, you can use Regex.Match(line, "[\S]+ [\S]+ [\S]+") for this file structure.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top