1

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!

4

4 回答 4

4

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

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

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

于 2013-07-08T12:38:10.070 回答
1
foreach my $string (@array) {
    $string =~ s/(\d{2})-(\d{2})-(\d{4})$/$1$2$3/;
}
于 2013-07-08T12:31:52.317 回答
1

要按照字面意思执行-从字符串中删除最后两个连字符-您可以这样写

$string =~ s/-([^-]*)-([^-]*)\z/$1$2/;

但在这种情况下,您可以简单地删除数字后面的所有连字符:

$string =~ s/\d\K-//g;
于 2013-07-08T12:49:08.343 回答
0

如果只有一个时什么都不应该做-

$s =~ s/-([^-]*)-([^-]*)\z/$1$2/;

$s = reverse($s);
$s =~ s/^([^-]*)-([^-]*)-/$1$2/;
$s = reverse($s);

$s = reverse( reverse($s) =~ s/^([^-]*)-([^-]*)-/$1$2/r );  # 5.14+

即使只有一个,所有这些都可以工作-

$s =~ s/-([^-]*)(?:-([^-]*))?\z/$1$2/;

$s =~ s/-([^-]*)\z/$1/ for 1..2;

$s =~ s/^.*\K-//s for 1..2;

$s = reverse($s);
$s =~ s/-// for 1..2;
$s = reverse($s);

$s = reverse($s);
$s =~ s/^([^-]*)-(?:([^-]*)-)?/$1$2/;
$s = reverse($s);

$s = reverse( reverse($s) =~ s/^([^-]*)-(?:([^-]*)-)?/$1$2/r );  # 5.14+

对于长字符串,reverse解决方案应该更快。对于短字符串,请考虑可读性。

于 2013-07-08T13:04:46.347 回答