如何删除字符串的最后两个“

编程入门 行业动态 更新时间:2024-10-24 12:25:04
如何删除字符串的最后两个“ - ”字符?(How to remove the last two occurrences of “-” character of a string?)

我有一个格式如下的字符串数组:

Ado-trastuzumab emtansine(HER2)02-22-2013

我只想删除最后两个“ - ”符号(从原始字符串的日期部分开始),以便药物名称(Ado-trastuzumab emtansine)不被改变。 现在我的正则表达式会删除所有的“ - ”符号:

foreach my $string (@array) { $string =~ tr/-//d; }

我希望输出结果如下所示:

Ado-trastuzumab emtansine(HER2)02222013

谢谢您的帮助!

I have an array of strings that are formatted as such:

Ado-trastuzumab emtansine(HER2)02-22-2013

I would like to remove the last two "-" symbols only (from the date part of the original string) so that the name of the drug (Ado-trastuzumab emtansine) is not altered. Right now my regex removes all "-" symbols:

foreach my $string (@array) { $string =~ tr/-//d; }

I would like the output to instead be the following:

Ado-trastuzumab emtansine(HER2)02222013

Thanks for the help!

最满意答案

您可以使用substr作为左值来仅将音译应用于字符串的特定部分:

substr($string, -10, 10) =~ tr/-//d;

在这种情况下,字符串的最后10个字母。

You can use substr as an lvalue to only apply the transliteration to a particular part of your string:

substr($string, -10, 10) =~ tr/-//d;

In this case, on the last 10 letters of the string.

更多推荐

本文发布于:2023-07-14 20:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1107231.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   两个

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!