perl how to remove last '_'?
In perl, how to write this regex ?
my $line = "job_name_1_" ; #end with '_'
$pattern = "_$"; # tried "\_$", still doesn't w开发者_开发知识库ork
if($line =~ m/$pattern/){
# remove last "_" ?
}
-#output should be "job_name"
how to do this ?
To remove the last underscore character, you just need to do this:
$line =~ s/_$//;
To remove a trailing underscore: (foo__ ⇒ foo_, foo_bar ⇒ foo_bar)
$line =~ s/_\z//;
To remove all trailing underscores: (foo__ ⇒ foo, foo_bar ⇒ foo_bar)
$line =~ s/_+\z//;
To remove the last underscore: (foo__ ⇒ foo_, foo_bar ⇒ foobar)
$line =~ s/_(?!.*_)//s;
$line =~ s/^.*\K_//s;
$subject =~ s/_(?=[^_]*$)//;
Sorry if someone else has also posted this :)
Try this:
$test = "test_";
$test = $1 if($test=~/(.*)_$/);
print $test
加载中,请稍侯......
精彩评论