Reputation: 141
I could split
$string = "AA_name"
into a @tmp array and grab $tmp[1] to obtain only the name and discard AA_. However, how would you do this with a s///? I don't know the regular expression to substitute everything preceding, and including, the underscore with nothing to leave me just the name. Something like
$n = $string =~ s/^_//;
But i'm fairly noob with regex.
Upvotes: 0
Views: 85
Reputation: 30577
You can use:
$string =~ s/^[^_]+_//;
This means replace any leading characters that are not underscores, plus an underscore, with nothing; which means that subsequent underscores are left in there, ie.
AA_My_Name ==> My_Name
If you want to removed up to the last '_' then use:
$string =~ s/^.+_//;
Other separators can be used. If you want to use '.' then remember to escape it with a '\'.
Note you do not have to assign the result to anything. s/// modifies the argument string.
Upvotes: 1
Reputation: 6204
You can do the following:
use strict;
use warnings;
my $string = "AA_name";
( my $n = $string ) =~ s/.*_//;
print $n;
Output:
name
If the separator is a dot, you can use s/.*\.//
(escaping the dot) or you can use a character class: s/.*[.]//
.
Upvotes: 0
Reputation: 3967
You can use the following regex
$string = "AA_name";
($name,undef) = $string =~ /(.*)_.*/;
print "name=$name", ",Val=" , $string;
Upvotes: 0