开发者

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_barfoo_bar)

$line =~ s/_\z//;

To remove all trailing underscores: (foo__foo, foo_barfoo_bar)

$line =~ s/_+\z//;

To remove the last underscore: (foo__foo_, foo_barfoobar)

$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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜