English 中文(简体)
如何从每克拉的体积中删除最后的数度。
原标题:How to remove last n number of numeric characters from a string in perl

我的情况是,在<代码>/性质之后,我需要删除最后的<代码>n。

例:

/iwmout/sourcelayer/iwm_service/iwm_ear_layer/pomoeron.xml@@/main/lsr_int_vnl46a/61

After the last /, I need the number 61 stripped out of the line so that the output is,

/iwmout/sourcelayer/iwm_service/iwm_ear_layer/pomoeron.xml@@/main/lsr_int_vnl46a/

我尝试使用ch,但只去掉最后的品格。 以上例子1。

最后一个部分,即61个,可能就象221或2或100个东西一样。 在<代码>/之后,我需要删除最后的数值。 是否可能在Perl?

问题回答

取消最后一位数的顶级替换:

my $str =  /iwmout/sourcelayer/iwm_service/iwm_ear_layer/pomoeron.xml@@/main/lsr_int_vnl46a/61 ;
$str =~ s/d+$//;

对应一系列数字,$对应线末。 他们被空洞所取代。

@Tim s对$str =~ s/d+$/的回答是正确的;然而,如果你想要删除最后的n数字特征,则你可以这样做:

my $s = "abc123456";
my $n = 3; # Just the last 3 chars.
$s =~ s/d{$n}$//; # $s == "abc123"
// Code to remove last n number of strings from a string.
// Import common lang jar
import org.apache.commons.lang3.StringUtils;

public class Hello {
    public static void main(String[] args) {
        String str = "Hello World";
        System.out.println(StringUtils.removeEnd(str, "ld"));
    }
}




相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签