解析.NET中的VB6代码

Sar*_*els 5 .net regex vb6 parsing

我有一个用C#编写的WPF项目,为了获得有关外部依赖项的一些信息,我需要解析一个VB6脚本.脚本的位置发生了变化,其内容发生了一些变化,但我感兴趣的主要代码的格式如下:

Select Case Fields("blah").Value
    Case "Some value"
        Fields("other blah").List = Lists("a list name")
    ...
End Select
Run Code Online (Sandbox Code Playgroud)

我需要从中提取出当字段'blah'设置为'某个值'时,字段'other blah'的列表会更改为列出'列表名'.我尝试使用谷歌搜索作为.NET库编写的VB6解析器,但还没有找到任何东西.有可能得到像这样的答案,我应该只使用正则表达式在VB6脚本中找到这样的代码,并提取我需要的数据吗?代码可以在子程序中找到,这样我就无法传递'blah','some value'并返回'other blah','list name'.我无法控制这个VB6脚本的内容.

Kob*_*obi 4

您可以通过几个步骤来解析它。请注意,正则表达式会丢失字符串和注释,因此请小心使用。

首先,我们将为这些Fields("Target").List = Lists("Value")行使用一个辅助类:

class ListData
{
    public string Target { get; set; }
    public string Value { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

输出模式:

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+)*
";
Run Code Online (Sandbox Code Playgroud)

接下来,我们可以尝试分两次解析文本(顺便说一句,代码有点难看,但相当基本):

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);
}
Run Code Online (Sandbox Code Playgroud)

现在你有了一本包含所有数据的字典。例如:

string s = data["foo"]["Some value2"].First().Value;
Run Code Online (Sandbox Code Playgroud)

这是一个工作示例: https: //gist.github.com/880148