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

Hello,

I'm using the following code to parse out Firmware Versions from an OID, and would like some input on my regex. It's only capturing 5 of the 9.

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @array = (); while (<DATA>) { push @array, $1 if m[Version\s+(.*)\,.*]i; push @array, $1 if m[Version\:\s+(.*)\s+PROM\.*]; } print Dumper (@array); # Array should be: # 12.2(15)T8 # 2.2.0.1 # 9.0.0.2 # 6.3(7) # 5.0.0.3 # 2.1.2.1 # E5.8.0.3 # 1.00.00 S # 12.0(14) __DATA__ Cisco Internetwork Operating System Software IOS (tm) 7200 Software (C7200-JS-M), Version 12.2(15)T8, RELEASE SOFT +WARE (fc2) TAC Support: http://www.cisco.com/tac Copyright (c) 1986-2003 by cisco Systems, Inc. Compiled Tue 09-Sep-03 18:47 by pwade Cabletron Systems, Inc. Firmware Version: 2.2.0.1 PROM Version: prom-1 +.1.0.5 RS 3000 - Riverstone Networks, Inc. Firmware Version: 9.0.0.2 PROM Ver +sion: prom-2.0.1.3 Cisco Systems WS-C5000 Cisco Catalyst Operating System Software, Version 6.3(7) Copyright (c) 1995-2002 by Cisco Systems X-Pedition Security Router XSR-1805 version 5.0.0.3 Cabletron ELS100-24TXG SW:2.1.2.1 2521 Testing - Foo Company Firmware Version: E5.8.0.3 PROM Version: pr +om-P1.3.2.1 Cabletron ELS100-24TX SW: 1.00.00 S Cisco Internetwork Operating System Software IOS (tm) 4000 Software (C4000-IS-M), Version 12.0(14), RELEASE SOFTWAR +E (fc1) Copyright (c) 1986-2000 by cisco Systems, Inc. Compiled Wed 01-Nov-00 00:06 by linda

Any thoughts?

Replies are listed 'Best First'.
Re: RegEx revision needed
by Abigail-II (Bishop) on Oct 09, 2003 at 12:30 UTC
    You know, it's very useful that beside the code and what you want it to output, you also print out what the code actually outputs. Now people have to download the code and run it to see what the differences are.

    Having said that, your regular expressions match lines that "Version", possibly a colon, whitespace, the actual version number, and then something else (comma, or whitespace followed by "PROM"). But the lines that aren't matched but of which you want them to match do have the "Version" and version number, but not the required trailing stuff. For instance, in

    Cisco Catalyst Operating System Software, Version 6.3(7)
    There's no comma or 'PROM' that can be matched, so the entire regex fails. You need a more accurate description of your version numbers.

    Abigail

      Sorry, I should have included the ouput:
      # Output: $VAR1 = '12.2(15)T8'; $VAR2 = '2.2.0.1'; $VAR3 = '9.0.0.2'; $VAR4 = 'E5.8.0.3'; $VAR5 = '12.0(14)';
Re: RegEx revision needed
by Wonko the sane (Curate) on Oct 09, 2003 at 13:48 UTC
    The entries you are missing in your output seem to be the numbers denoted by 'SW:'.
    Neither of your regexes attempt to match for this at all. The 'SW;'
    numbers also seem to have a unique trait that spaces are allowed as part
    of the data. 'SW: 1.00.00 S'

    The following is a modification of your loop that gives the output you specified.

    while (<DATA>) { push @array, $1 if ( /Version:?\s*([^\s,]+)/i ); push @array, $1 if ( /SW:?\s*([^\n\r]+)/i ); }
    Outputs:
    :!./test.pl $VAR1 = '12.2(15)T8,'; $VAR2 = '2.2.0.1'; $VAR3 = '9.0.0.2'; $VAR4 = '6.3(7)'; $VAR5 = '5.0.0.3'; $VAR6 = '2.1.2.1'; $VAR7 = 'E5.8.0.3'; $VAR8 = '1.00.00 S'; $VAR9 = '12.0(14)';
    Hope that helps.

    Wonko