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

Hi. I need to process some SNMP OIDs of the form 1.3.6.1.4.1.1012.4.1 (variable length) and get me everything except the last number. for instance:
print "oid[$oidfila]\n" oid[1.3.6.1.4.1.1012.170.2.40.1.1.1.0.4194288]
I need to extract the 4194288 from $oidfila and get me the rest... I've tried:
$oidfila =~ s/^[0-9\.]+(\.[0-9]+)$//;
but it just gets me blanks. You help is more than appreciated. Regards, Leo

Replies are listed 'Best First'.
Re: regexp problem - extract all but last part of SNMP OID
by Eliya (Vicar) on Nov 21, 2011 at 16:23 UTC
    $oidfila =~ s/^[0-9\.]+(\.[0-9]+)$//;

    This is already a good start, but you're matching - and thus substituting - too much. Leave off the first part and things will be ok:

    $oidfila =~ s/\.[0-9]+$//;
      Alternatively, if the initial condition is essential, you can use a capture on the lead in. This is inefficient, but necessary without variable-length look behind support.

      $oidfila  =~ s/(^[0-9\.]+)\.[0-9]+$/$1/;

        Which, according to perlre, can be written more efficiently with the "keep" construct:

        $oidfila =~ s/^[\d.]+\K\.\d+$//;