Skip to content

Instantly share code, notes, and snippets.

@kobi
Created March 21, 2011 20:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kobi/880148 to your computer and use it in GitHub Desktop.
Save kobi/880148 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string vb = @"
Select Case Fields(""blah"").Value
Case ""Some value""
Fields(""other blah"").List = Lists(""a list name"")
Case ""Some value2""
Fields(""other blah2a"").List = Lists(""a list name2a"")
Fields(""other blah2b"").List = Lists(""a list name2b"")
Case ""Some value3""
Fields(""other blah3"").List = Lists(""a list name3"")
End Select
Select Case Fields(""foo"").Value
Case ""Some value""
Fields(""other foo"").List = Lists(""a list foo"")
Case ""Some value2""
Fields(""other foo2"").List = Lists(""a list foo2"")
Case ""Some value3""
Fields(""other foo3"").List = Lists(""a list foo3"")
End Select
";
string patternSelectCase = @"
Select\s+Case\s+Fields\(""(?<CaseField>[\w\s]+)""\)\.Value
(?<Cases>.*?)
End\s+Select
";
string patternCase = @"
Case\s+""(?<Case>[\w\s]+)""\s+
(?:Fields\(""(?<Target>[\w\s]+)""\)\.List\s*=\s*Lists\(""(?<Value>[\w\s]+)""\)\s+)*
";
MatchCollection matches = Regex.Matches(vb, patternSelectCase, RegexOptions.IgnoreCase |
RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline);
Console.WriteLine(matches.Count);
var data = new Dictionary<String, Dictionary<String, List<ListData>>>();
foreach (Match match in matches)
{
var caseData = new Dictionary<String, List<ListData>>();
string caseField = match.Groups["CaseField"].Value;
string cases = match.Groups["Cases"].Value;
MatchCollection casesMatches = Regex.Matches(cases, patternCase, RegexOptions.IgnoreCase |
RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline);
foreach (Match caseMatch in casesMatches)
{
string caseTitle = caseMatch.Groups["Case"].Value;
var targetCaptures = caseMatch.Groups["Target"].Captures.Cast<Capture>();
var valueCaptures = caseMatch.Groups["Value"].Captures.Cast<Capture>();
caseData.Add(caseTitle, targetCaptures.Zip(valueCaptures, (t, v) => new ListData
{
Target = t.Value,
Value = v.Value
}).ToList());
}
data.Add(caseField, caseData);
}
Console.WriteLine(data["foo"]["Some value2"].First().Value);
Console.ReadKey();
}
}
class ListData
{
public string Target { get; set; }
public string Value { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment