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

hi all, i am facing a strange problem
here is my string
$line=SNMPv2-MI::enterprises.343.2.10.3.5.100.1.0=INT.
I tried using split function , but was able to extract only
enterprises.343.2.10.3.5.100.1.0=INT
but what i need is just .343.2.10.3.5.100.1.0
That is anything after the word enterprises and before = sign.

could anyone help me in this ?

Replies are listed 'Best First'.
Re: cut a string in a string
by izut (Chaplain) on Feb 03, 2006 at 17:33 UTC
    I think a better solution than 'split' is using regex. Something like this (untested):
    $line =~ m/^\S+\.(.*)=\S+$/;

    Igor 'izut' Sutton
    your code, your rules.

Re: cut a string in a string
by serf (Chaplain) on Feb 03, 2006 at 17:57 UTC
    If you are matching many instances of this (say reading a log file that is 700MB or more and checking each line for this string) then you are likely to find that the performance improves if you:
    • tie the match down to the bare minimum of wildcard characters,
    • specify everything that you know is going to be in the match,
    • and also anchor the match where possible to the beginning and end of the string (or line) so it doesn't have to try iterating over the string...
    use strict; use warnings; my $line = "SNMPv2-MI::enterprises.343.2.10.3.5.100.1.0=INT"; my $match = $1 if $line =~ /^SNMPv2-MI::enterprises([0-9\.]+)=INT$/;
Re: cut a string in a string
by phaylon (Curate) on Feb 03, 2006 at 17:20 UTC
    How have you tried? It would make more sense to tell you where you took the wrong road instead of just giving you a solution.

    Ordinary morality is for ordinary people. -- Aleister Crowley
Re: cut a string in a string
by smokemachine (Hermit) on Feb 03, 2006 at 18:02 UTC
    perl -e '$line="SNMPv2-MI::enterprises.343.2.10.3.5.100.1.0=INT"; print $1 if $line=~/^[\w|\d|-]*::\w*.(((\d{1,3})\.?){8})/'
A reply falls below the community's threshold of quality. You may see it by logging in.