解析VB6 code。在.NET [英] parsing VB6 code in .NET
问题描述
我已经写在C#中的WPF项目,以获取有关外部依赖的一些信息,我需要解析VB6的脚本。该脚本的位置变化,其含量改变了一些,但主要code我感兴趣的将是格式为:
I have a WPF project written in C#, and in order to get some information about an external dependency, I need to parse a VB6 script. The script's location changes and its content changes some, but the main code I'm interested in will be of the format:
Select Case Fields("blah").Value
Case "Some value"
Fields("other blah").List = Lists("a list name")
...
End Select
我需要从这种提取物,当现场嗒嗒设置为某些价值,名单现场等嗒嗒的变化,列出清单名称。我试图谷歌搜索周围VB6的语法分析器写成一个.NET库,但没有发现任何东西。在得到像<一个答案的风险href="http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454">this 之一,我应该只是使用普通的EX pressions找到这样的code在VB6脚本,并提取我需要的数据?在code是一个子程序,这样我可以不通过在胡说,一些价值,并取回等胡说,列表名称中。我已经在这个VB6脚本的内容没有控制权。
I need to extract from this that when field 'blah' is set to 'some value', the list for field 'other blah' changes to list 'a list name'. I tried Googling around for a VB6 parser written as a .NET library but haven't found anything yet. At the risk of getting an answer like this one, should I just use regular expressions to find the code like this in the VB6 script, and extract the data I need? The code is found in a subroutine such that I can't pass in 'blah', 'some value' and get back 'other blah', 'a list name'. I have no control over the contents of this VB6 script.
推荐答案
您可以通过几个步骤解析它。请注意,正则表达式错过字符串和注释,所以小心使用。
You can parse it in a few steps. Please note the regex misses strings and comments, so use with care.
首先,我们将使用一个辅助类的字段(目标)列出=列表(值)
行:
First, we'll use a helper class for the Fields("Target").List = Lists("Value")
lines:
class ListData
{
public string Target { get; set; }
public string Value { get; set; }
}
输出方式:
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+)*
";
接下来,我们可以尝试分析文本,分两次(在code是有点丑,顺便说一下,但还算基本):
Next, we can try to parse the text in two passes (the code is a little ugly, by the way, but fairly basic):
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);
}
现在你有一个与所有的数据字典。例如:
Now you have a dictionary with all data. For example:
string s = data["foo"]["Some value2"].First().Value;
下面是一个工作的例子: https://gist.github.com/880148
Here's a working example: https://gist.github.com/880148
这篇关于解析VB6 code。在.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!