monkie has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I'm expecting to pull from a win32 reg file of the uninstall keys, the Name, Version and Manufacturer of the software installed.


My perl:
#!/usr/bin/perl use strict; open (DATAFILE, "<", 'test.txt') or die "Couldn't open file for readin +g: $!\n"; my $temp; $/ = ""; ## Paragraph mode my $display_name; my $version; while( <DATAFILE> ) { m[ (?= .*? \"DisplayName\"=\"([a-zA-Z0-9. -]*)) (?= .*? \"DisplayVersion\"=\"([a-zA-Z0-9 .-]*)) (?= .*? \"Publisher\"=\"([a-zA-Z0-9 .-]*)) ]xsm; print "Name:$1\nVersion:$2\nPublisher:\n\n"; } close(DATAFILE);


My Output:
Name: Version: Publisher: Name: Version: Publisher: Name: Version: Publisher: Name:Adobe SVG Viewer 3.0 Version: 3.0 Publisher: Name: Version: Publisher: Name: Version: Publisher: Name:Example Display Name Version:v7.5.2 Publisher: Name: Version: Publisher:
The data file:
Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstal +l] "DisplayName"="" "UninstallString"="C:\\PROGRA~1\\CISCOS~1\\VPNCLI~1\\Profiles\\PROGRA~ +1\\CISCOS~1\\VPNCLI~1\\profiles\\UNWISE.EXE C:\\PROGRA~1\\CISCOS~1\\V +PNCLI~1\\Profiles\\PROGRA~1\\CISCOS~1\\VPNCLI~1\\profiles\\INSTALL.LO +G" [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstal +l\AddressBook] @="" [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstal +l\Adobe SVG Viewer] "DisplayName"="Adobe SVG Viewer 3.0" "DisplayVersion"=" 3.0" "DisplayIcon"="C:\\Program Files\\Common Files\\Adobe\\SVG Viewer 3.0\ +\Uninstall\\SetupRsrc.dll,-200" "VersionMajor"=dword:00000003 "VersionMinor"=dword:00000000 "InstallLocation"="C:\\WINDOWS\\system32\\Adobe\\SVG Viewer 3.0" "UninstallString"="C:\\Program Files\\Common Files\\Adobe\\SVG Viewer +3.0\\Uninstall\\Winstall.exe -u -fC:\\Program Files\\Common Files\\Ad +obe\\SVG Viewer 3.0\\Uninstall\\Install.log" "UninstallDir"="C:\\Program Files\\Common Files\\Adobe\\SVG Viewer 3.0 +\\Uninstall" "Publisher"="Adobe Systems, Inc." "RegCompany"="Adobe Systems Inc" "URLInfoAbout"="http://www.adobe.com/" "URLUpdateInfo"="http://www.adobe.com/svg/main.html" [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstal +l\Artemis 7.2.1 ODBC] "NoRemove"=dword:00000001 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstal +l\Standalone] "UninstallString"="C:\\WINDOWS\\IsUninst.exe -f\"C:\\Program Filesone. +isu\"" "DisplayName"="This is-not SHOWING" [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstal +l\exampler] "DisplayName"="Example Display Name" "UninstallString"="c:\\applications\\uninst.exe" "DisplayVersion"="v7.5.2" "URLInfoAbout"="http://something.htm" "Publisher"="NOT caught" [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstal +l\Branding] "QuietUninstallString"="Rundll32 IedkCS32.dll,BrandCleanInstallStubs" "RequiresIESysFile"="100.0"
As you can see from my results, I'm not catching everything, bearing in mind that the order of the 3 fields which I'm looking for may differ for each entry. Please can someone show me the folly of my ways?

Replies are listed 'Best First'.
Re: Some regex magic
by przemo (Scribe) on May 08, 2009 at 13:35 UTC

    I'd choose another way and use one of myriad Config::* CPAN modules. For example, with Config::Tiny you can do something similar to (after removing everything before the first section in your data file):

    use warnings; use strict; use Config::Tiny; my $cfg = Config::Tiny->read('test.txt') or die "Couldn't read file: $ +Config::Tiny::errstr"; for my $sect (values %$cfg) { my ($name, $version, $publisher) = map { $sect->{$_} } qw("Display +Name" "DisplayVersion" "Publisher"); print "Name: $name\nVersion: $version\nPublisher: $publisher\n"; + }
Re: Some regex magic
by johngg (Canon) on May 08, 2009 at 13:14 UTC

    I would use three separate regular expressions rather than trying to use just one more complex one using look-aheads. Also, double-quotes are not metacharacters so need not be escaped.

    use strict; use warnings; open my $regFH, q{<}, \ <<'EOD' or die qq{open: << HEREDOC: $!\n}; Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstal +l] "DisplayName"="" "UninstallString"="C:\\PROGRA~1\\CISCOS~1\\VPNCLI~1\\Profiles\\PROGRA~ +1\\CISCOS~1\\VPNCLI~1\\profiles\\UNWISE.EXE C:\\PROGRA~1\\CISCOS~1\\V +PNCLI~1\\Profiles\\PROGRA~1\\CISCOS~1\\VPNCLI~1\\profiles\\INSTALL.LO +G" [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstal +l\AddressBook] @="" [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstal +l\Adobe SVG Viewer] "DisplayName"="Adobe SVG Viewer 3.0" "DisplayVersion"=" 3.0" "DisplayIcon"="C:\\Program Files\\Common Files\\Adobe\\SVG Viewer 3.0\ +\Uninstall\\SetupRsrc.dll,-200" "VersionMajor"=dword:00000003 "VersionMinor"=dword:00000000 "InstallLocation"="C:\\WINDOWS\\system32\\Adobe\\SVG Viewer 3.0" "UninstallString"="C:\\Program Files\\Common Files\\Adobe\\SVG Viewer +3.0\\Uninstall\\Winstall.exe -u -fC:\\Program Files\\Common Files\\Ad +obe\\SVG Viewer 3.0\\Uninstall\\Install.log" "UninstallDir"="C:\\Program Files\\Common Files\\Adobe\\SVG Viewer 3.0 +\\Uninstall" "Publisher"="Adobe Systems, Inc." "RegCompany"="Adobe Systems Inc" "URLInfoAbout"="http://www.adobe.com/" "URLUpdateInfo"="http://www.adobe.com/svg/main.html" [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstal +l\Artemis 7.2.1 ODBC] "NoRemove"=dword:00000001 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstal +l\Standalone] "UninstallString"="C:\\WINDOWS\\IsUninst.exe -f\"C:\\Program Filesone. +isu\"" "DisplayName"="This is-not SHOWING" [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstal +l\exampler] "DisplayName"="Example Display Name" "UninstallString"="c:\\applications\\uninst.exe" "DisplayVersion"="v7.5.2" "URLInfoAbout"="http://something.htm" "Publisher"="NOT caught" [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstal +l\Branding] "QuietUninstallString"="Rundll32 IedkCS32.dll,BrandCleanInstallStubs" "RequiresIESysFile"="100.0" EOD { local $/ = q{}; while( <$regFH> ) { next unless m{^\[HKEY}; my $displayName = m{"DisplayName"="([^"]+)} ? do { my $s = $1; $s =~ s{^\s+}{}; $s =~ s{\s+$}{}; $s; } : q{Not found!}; my $displayVersion = m{"DisplayVersion"="([^"]+)} ? do { my $s = $1; $s =~ s{^\s+}{}; $s =~ s{\s+$}{}; $s; } : q{Not found!}; my $publisher = m{"Publisher"="([^"]+)} ? do { my $s = $1; $s =~ s{^\s+}{}; $s =~ s{\s+$}{}; $s; } : q{Not found!}; print qq{Name: $displayName\n}, qq{Version: $displayVersion\n}, qq{Publisher: $publisher\n\n}; } } close $regFH or die qq{close: << HEREDOC: $!\n};

    The output.

    Name: Not found! Version: Not found! Publisher: Not found! Name: Not found! Version: Not found! Publisher: Not found! Name: Adobe SVG Viewer 3.0 Version: 3.0 Publisher: Adobe Systems, Inc. Name: Not found! Version: Not found! Publisher: Not found! Name: This is-not SHOWING Version: Not found! Publisher: Not found! Name: Example Display Name Version: v7.5.2 Publisher: NOT caught Name: Not found! Version: Not found! Publisher: Not found!

    I hope this is helpful.

    Cheers,

    JohnGG

    Update: Fixed typo, changed mot to not

Re: Some regex magic
by philipbailey (Curate) on May 08, 2009 at 14:04 UTC
    Another suggestion, orthogonal to those given so far, is to obtain your data directly from the Windows registry. There are various modules to do this, although I have had most luck with Win32::TieRegistry.
Re: Some regex magic
by johngg (Canon) on May 09, 2009 at 22:00 UTC

    Although I advocated using three separate regular expressions in my first reply I wondered whether the OP's use of three look-aheads with captures in a single regular expression could work. The look-aheads had to cope with the fact that none, all or any combination in between of the three relevant items might exist. Trying various combinations of non-greedy and zero-or-one quantifiers inside each look-ahead resulted in a lot of head scratching but no useful output. Finally, it occurred to me to try putting the zero-or-one quantifiers on the look-aheads themselves, thus making each look-ahead optional. That did the trick.

    The output.

    I hope this is of interest.

    Cheers,

    JohnGG

    Update: Fixed typo, s{scatching}{scratching}

      Thank you so much - I decided to go with your first suggestion which is working very well - doing exactly what it's meant to do!
Re: Some regex magic
by bichonfrise74 (Vicar) on May 08, 2009 at 22:53 UTC
    Try this...
    #!/usr/bin/perl use strict; $/ = "\n\n"; while( <DATA> ) { my ($name) = $_ =~ /DisplayName\"\=\"(.*)\"/; my ($version) = $_ =~ /VersionMajor\"\=(.*)/; my ($publisher) = $_ =~ /Publisher\"\=\"(.*)\"/; $name = defined($name) ? $name : "n/a"; $version = defined($version) ? $version : "n/a"; $publisher = defined($publisher) ? $publisher : "n/a"; print "\nName: $name\nVersion: $version\nPublisher: $publisher\n"; } __DATA__ [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstal +l] "DisplayName"="" "UninstallString"="C:\\PROGRA~1\\CISCOS~1\\VPNCLI~1\\Profiles\\PROGRA~ +1\\CISCOS~1\\VPNCLI~1\\profiles\\UNWISE.EXE C:\\PROGRA~1\\CISCOS~1\\V +PNCLI~1\\Profiles\\PROGRA~1\\CISCOS~1\\VPNCLI~1\\profiles\\INSTALL.LO +G" [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstal +l\Adobe SVG Viewer] "DisplayName"="Adobe SVG Viewer 3.0" "DisplayVersion"=" 3.0" "DisplayIcon"="C:\\Program Files\\Common Files\\Adobe\\SVG Viewer 3.0\ +\Uninstall\\SetupRsrc.dll,-200" "VersionMajor"=dword:00000003 "VersionMinor"=dword:00000000 "InstallLocation"="C:\\WINDOWS\\system32\\Adobe\\SVG Viewer 3.0" "UninstallString"="C:\\Program Files\\Common Files\\Adobe\\SVG Viewer3 +.0\\Uninstall\\Winstall.exe -u -fC:\\Program Files\\Common Files\\Ado +be\\SVG Viewer 3.0\\Uninstall\\Install.log" "UninstallDir"="C:\\Program Files\\Common Files\\Adobe\\SVG Viewer 3.0 +\\Uninstall" "Publisher"="Adobe Systems, Inc." "RegCompany"="Adobe Systems Inc" "URLInfoAbout"="http://www.adobe.com/" "URLUpdateInfo"="http://www.adobe.com/svg/main.html"