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

Does someone have a regex to extract
00.01.00 from:
CMTS_V03.01.01.41, <<HW_REV: 1.3; VENDOR: ARRIS; BOOTR: V00.01.00>>
The version number isn't always the same! Thanks

Replies are listed 'Best First'.
Re: RegEx help
by jeffa (Bishop) on Nov 03, 2003 at 13:25 UTC
    How about:
    my ($ver) = $str =~ /V([0-9.]+)>>$/;
    UPDATE:
    or you could reverse the string and use substr (if the version number will always be 8 chars long, that is):
    my $ver = substr(reverse($str),2,8);
    UPDATE:
    From liz: my $ver = substr($str,-9,8);

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: RegEx help
by Abigail-II (Bishop) on Nov 03, 2003 at 13:33 UTC
    That problem is way to vaguely defined. I could come up with a dozen regexes that extract '00.01.00' from the above string, but each of them might fail with any other string.

    You use regexes to extract patterns. But you cannot define a pattern by giving just one example, especially not if you say The version number isn't always the same!. There are thousands of ways to describe version numbers.

    Unless you tell us what kind of pattern the version numbers have (or by which pattern the version numbers can be found in the strings), there's no guarantee what any of the doubtless many guesses presented in this thread will actually work on your production data.

    You might get lucky, and you pick a regex that will work. But how will you know?

    Abigail

Re: RegEx help
by broquaint (Abbot) on Nov 03, 2003 at 13:28 UTC
    If your format is consistent then you could just do a simple match after the final 'V' and before the '>' for digits and dots e.g
    my $s = "CMTS_V03.01.01.41, <<HW_REV: 1.3; VENDOR: ARRIS; BOOTR: V00.0 +1.00>>"; my($vn) = $s =~ m{V((?:\d+\.?)+)>}; print "version number: $vn"; __output__ version number: 00.01.00
    See. perlre for more information on regular expressions in perl.
    HTH

    _________
    broquaint

Re: RegEx help
by delirium (Chaplain) on Nov 03, 2003 at 13:48 UTC
    I'll bite. Judging by your one sample, it looks like the version information is surrounded by << and >>, and that the attribute you are looking for is the BOOTR version.

    Your question implies that each version number will start with V, but we don't know what else it will contain, only that it will end with >>, or possibly a semi-colon to start another version attribute. Given that all that is true, this RE should work:

    /<<.*BOOTR: V(.+)?(?:;|>>)/; print $1;

    This will explicitly look for the BOOTR attribute, and stop at the first ; or >>.

Re: RegEx help
by bradcathey (Prior) on Nov 03, 2003 at 14:12 UTC
    How's this:
    #!/usr/bin/perl -w print "Content-type: text/html\n\n"; use strict; my $version = "CMTS_V03.01.01.41, <<HW_REV: 1.3; VENDOR: ARRIS; BOOTR: + V00.01.0"; $version =~ s/^(.+)V([\d\.]+)$/$2/; print $version,"\n";
    But will be anxious to read the other ways.

    Update: Abigail-II makes an interesting point, but I assumed that the version number, even if different, would appear in the same place (end of the string), with the "V" prefix. But I'm open...

Re: RegEx help
by Anonymous Monk on Nov 03, 2003 at 15:02 UTC
    $_ = q|CMTS_V03.01.01.41, <<HW_REV: 1.3; VENDOR: ARRIS; BOOTR: V00.01. +00>>|; # change this if the version number changes. s/.*/00.01.00/; print;