0

我有一个包含 429 个 MFC 资源文件的列表,我必须为其生成一个快捷方式列表,这些文件是包含 & 符号的按钮(例如 BUTTON“&Close”),表示 ALT-C 是关闭该特定对话框的快捷方式。

问题是资源文件包含许多不同的对话框,格式如下:

IDD_VIDEO DIALOG  0, 0, 471, 187
...
BEGIN
    ...
    PUSHBUTTON      "&Close",IDC_CLOSE,89,166,53,14
    ...
END

我想提取的格式是“&Close”(或理想情况下是“ALT-C &Close”)和其他带有快捷方式的标签的列表,按它们所在的对话框(例如 IDD_VIDEO)划分。正则表达式似乎是最好的解决方案,但我还没有为此找到一个有效的正则表达式。

4

1 回答 1

1

感谢您添加的规格。这应该有效:

^                  # Start of line
(IDD_\w+)          # Alphanumeric identifier, starting with IDD_
\s+DIALOG\b        # followed by "DIALOG"
((?:(?!^END\b).)*) # and any number of characters unless there's an END in-between

IDD_whatever这将匹配从直到下一个的整个部分END。然后您需要获取该字符串并将以下正则表达式应用于它:

"([^"]*&[^"]*)"  # String containing at least one &

这是一个 C# 示例:

Regex sectionRegex = new Regex(
    @"^                 # Start of line
    (IDD_\w+)           # Alphanumeric identifier, starting with IDD_
    \s+DIALOG\b         # followed by ""DIALOG""
    ((?:(?!^END\b).)*)  # and any number of characters unless there's an END in-between",
    RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace);

Regex altCRegex = new Regex(
    @"""([^""]*&[^""]*)"" # String containing at least one &", 
    RegexOptions.IgnorePatternWhitespace);

Match matchResults = sectionRegex.Match(subjectString);
while (matchResults.Success) {
    identifier = matchResults.Groups[1].Value;
    section = matchResults.Groups[2].Value;
    Match sectionResults = altCRegex.Match(section);
    while (sectionResults.Success) {
        altCString = sectionResult.Groups[1].Value;
        sectionResults = sectionResults.NextMatch();
    }
    matchResults = matchResults.NextMatch();
} 

identifier当然,这个代码片段对and没有任何作用altCString,但我想你明白了。

于 2012-01-24T10:45:25.103 回答