I have a string like this <name>
, I want to chop off the <>
and take out only name
and put it into a variable.
How to do it using perl "split" function?
I have a string like this <name>
, I want to chop off the <>
and take out only name
and put it into a variable.
How to do it using perl "split" function?
I'm not too sure why you'd want to use split for that... but here it goes:
> perl -le'$str="<string>";print split("<",(split(">",$str))[0]);'
string
In answer to you question about how to do it using split then you can do this:
$input ="<string>";
print split(/[<>]/,$input);
A better method however would be
$input = "<string>";
print $input =~ /<(.*)>/;
You can use regex and matching in list context:
my $s = "<name>";
my ($name) = $s =~ /<(.*)>/;
Don't use split. That's like using the wrong end of a screwdriver to hammer in nails. I'd do it in some step with a match where you capture the part that you want in list context:
my( $var ) = $input =~ /<(.*?)>/;
Alternatively, you could just remove the brackets with one of these:
$input =~ tr/<>//;
$input =~ s/[<>]//g;