What is best practice for parsing the following blocks in c#-regex?
BLOCK:1 { name:Apple, value:Juice, name:xxx, value:yyy, name:Apple, value:Juice }
BLOCK:2 { name:Banana, value:Smell }
BLOCK:3 { }
Edited: flexible blocks
What is best practice for parsing the following blocks in c#-regex?
BLOCK:1 { name:Apple, value:Juice, name:xxx, value:yyy, name:Apple, value:Juice }
BLOCK:2 { name:Banana, value:Smell }
BLOCK:3 { }
Edited: flexible blocks
The following should get you the expression and the groups 'id', 'name' and 'value' for further use.
BLOCK:(?<id>[0-9+]) { name:(?<name>.+), value:(?<value>.+) }
You can then split the block by newlines, and try to parse each line with the regex, then process them if it's a succesful match.
Regex pattern = new Regex("BLOCK:(?<id>[0-9]+) { name:(?<name>.+), value:(?<value>.+) }");
foreach(var line in block.Split('\n'))
{
Match match = pattern.Match(line);
if(match.Success)
{
Process(match.Groups["value"].Value);
}
}
edit
Use something like BLOCK:(?<id>[0-9]+) {(?<inner>.+)}
to do the first match.
Then check this question for further info
This might be too simple to require Antlr, but it wouldn't hurt to check it out for future reference.
If you need to parse anything at all reasonably complex then you just can't beat http://www.antlr.org/
And, of course, it's associated GUI - http://www.antlr.org/works/index.html
A picture speaks a thousand words - http://www.antlr.org/works/screenshots/editor.jpg
Simpler to code, simpler to debug ...