0

I have a list of 429 MFC resource files that I have to generate a list of shortcuts for, which would be buttons containing an ampersand symbol (e.g. BUTTON "&Close") indicating that ALT-C is the shortcut for closing that particular dialog.

The problem is that the resource files contain many different dialogs formatted as such:

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

The format I would like to pull out would be a list of "&Close" (Or ideally "ALT-C &Close") and other labels with shortcuts, sectioned by which dialog they're under (e.g. IDD_VIDEO). Regex seems like the best solution but I haven't been able to figure a working regex out for this yet.

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
Sean Chapman
  • 322
  • 1
  • 10

1 Answers1

1

Thanks for the added specifications. This should work:

^                  # 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

This will match the entire section from IDD_whatever until the next END. Then you need to take that string and apply the following regex to it:

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

Here's a C# example:

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();
} 

Of course this code snippet doesn't do anything with identifier and altCString, but I think you get the idea.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • Thanks for your answer. I am using Notepad++ at the moment to test the regex strings but I will probably code a C# tool to process all the files afterward. The dialog identifiers all start with IDD_ and end with DIALOG (e.g. "IDD_VIDEO DIALOG 0, 42, 45, 89" or "IDD_MAIN DIALOGEX 0,65,89,89) As for the number of strings with & inside them, there can be many lines between the BEGIN/END tags containing one & tag. Hope this helps clarify what I'm trying to achieve. – Sean Chapman Jan 24 '12 at 10:57
  • 1
    Notepad++ has very limited regex support. As far as I know, it does not support regex matches that span over multiple lines. Thanks for the clarification; the regex should already be working correctly, but I'll refine it a bit. – Tim Pietzcker Jan 24 '12 at 11:03
  • I've written a C# app to run the regex and it seems to be working quite well except that when printing the "Groups[2]" values out, it only gives me the last string in the BEGIN/END tags which contains a &, when I'm looking to print out all of the strings in the BEGIN/END tags containing an &. "Groups[0].Value" shows that it's getting from the start of the dialog to the last string with an & in the dialog, but only printing the last string with & in. I hope you can help – Sean Chapman Jan 24 '12 at 11:58
  • Ah, misunderstanding on my part. So each `IDD_whatever` can have multiple "Alt-C" strings within its `BEGIN`-`END` section, and you need all of them? Then you need a different approach. Just a second. – Tim Pietzcker Jan 24 '12 at 12:14
  • Yep that's correct. All the strings containing an & symbol between the BEGIN and END tags – Sean Chapman Jan 24 '12 at 12:16