perl 去掉一个字串的后面n个字母

qq413187589 2010-10-14 05:04:44
perl 去掉一个字串的后面n个字母
怎么弄?谢谢!
...全文
144 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
fibbery 2010-10-14
  • 打赏
  • 举报
回复
经常配合length使用:
length EXPR
length Returns the length in *characters* of the value of EXPR. If EXPR
is omitted, returns length of $_. If EXPR is undefined, returns
"undef".

This function cannot be used on an entire array or hash to find
out how many elements these have. For that, use "scalar @array"
and "scalar keys %hash", respectively.

Like all Perl character operations, length() normally deals in
logical characters, not physical bytes. For how many bytes a
string encoded as UTF-8 would take up, use
"length(Encode::encode_utf8(EXPR))" (you'll have to "use Encode"
fibbery 2010-10-14
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 iambic 的回复:]
可以用substr
[/Quote]

支持用substr。

substr EXPR,OFFSET,LENGTH,REPLACEMENT
substr EXPR,OFFSET,LENGTH
substr EXPR,OFFSET
Extracts a substring out of EXPR and returns it. First character
is at offset 0, or whatever you've set $[ to (but don't do
that). If OFFSET is negative (or more precisely, less than $[),
starts that far from the end of the string. If LENGTH is
omitted, returns everything to the end of the string. If LENGTH
is negative, leaves that many characters off the end of the
string.

                my $s = "The black cat climbed the green tree";
my $color = substr $s, 4, 5; # black
my $middle = substr $s, 4, -11; # black cat climbed the
my $end = substr $s, 14; # climbed the green tree
my $tail = substr $s, -4; # tree
my $z = substr $s, -4, 2; # tr

You can use the substr() function as an lvalue, in which case
EXPR must itself be an lvalue. If you assign something shorter
than LENGTH, the string will shrink, and if you assign something
longer than LENGTH, the string will grow to accommodate it. To
keep the string the same length, you may need to pad or chop
your value using "sprintf".

If OFFSET and LENGTH specify a substring that is partly outside
the string, only the part within the string is returned. If the
substring is beyond either end of the string, substr() returns
the undefined value and produces a warning. When used as an
lvalue, specifying a substring that is entirely outside the
string raises an exception. Here's an example showing the
behavior for boundary cases:

                my $name = 'fred';
substr($name, 4) = 'dy'; # $name is now 'freddy'
my $null = substr $name, 6, 2; # returns "" (no warning)
my $oops = substr $name, 7; # returns undef, with warning
substr($name, 7) = 'gap'; # raises an exception

An alternative to using substr() as an lvalue is to specify the
replacement string as the 4th argument. This allows you to
replace parts of the EXPR and return what was there before in
one operation, just as you can with splice().

                my $s = "The black cat climbed the green tree";
my $z = substr $s, 14, 7, "jumped from"; # climbed
# $s is now "The black cat jumped from the green tree"

Note that the lvalue returned by the 3-arg version of substr()
acts as a 'magic bullet'; each time it is assigned to, it
remembers which part of the original string is being modified;
for example:

                $x = '1234';
for (substr($x,1,2)) {
$_ = 'a'; print $x,"\n"; # prints 1a4
$_ = 'xyz'; print $x,"\n"; # prints 1xyz4
$x = '56789';
$_ = 'pq'; print $x,"\n"; # prints 5pq9
}

Prior to Perl version 5.9.1, the result of using an lvalue
multiple times was unspecified.
iambic 2010-10-14
  • 打赏
  • 举报
回复
可以用substr
masmaster 2010-10-14
  • 打赏
  • 举报
回复
echo 123456 | perl -pe 's/...$//'
zuo_shen 2010-10-14
  • 打赏
  • 举报
回复

正则表达式

37,738

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧