cut a string in a string
Anonymous Monk
created: 2006-02-03 12:17:31
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 ?
Re: cut a string in a string
created: 2006-02-03 12:20:55
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
created: 2006-02-03 12:33:14
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
created: 2006-02-03 12:57:40
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
created: 2006-02-03 13:02:31
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})/'
Re: cut a string in a string
created: 2006-02-04 05:15:51
Thanks everybody. I tried split function and got the results. I know this is a long procedure and bad programming but i learnt a lot in this process. Here is what i have done First step input is SNMPv2-SMI::enterprises.343.2.10.3.5.100.1.0 = INTEGER: 5 output is SNMPv2-SMI::enterprises.343.2.10.3.5.100.1.0=INTEGER:5 removed all the intermediate spaces using $line=~s/\s//g; Then replaced all the . with : so that i get result as SNMPv2-SMI::enterprises:343:2:10:3:5:100:1:0:INTEGER:5 Then put this string in a array. Read through the array. concatinate only numbers until the word INTEGER is reached. finally i will get .343.2.10.3.5.100.1.0
Re^2: cut a string in a string
created: 2006-02-04 13:03:56
Dont do things that complicated. Perl has one of the best regex machines. Use it to get what you want as it has been explained before. Using perl without that mighty mechanisms is as using a knife without an egde. I am exagerating but...

perlmonks.org content © perlmonks.org and Anonymous Monk, izut, phaylon, serf, smokemachine

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03