Для продвижения моей идеи использовать rindex для получения
1) "ab.cde.fg"
2) ".hi"
из "ab.cde.fg.hi"
, я написал этот скрипт для упрощения экспериментов:
use strict;
use diagnostics;
use warnings;
use English;
my @tests = (
[ 'ab.cde.fg.hi', 'ab.cde.fg|.hi' ]
, [ 'abxcdexfg.hi', 'abxcdexfg|.hi' ]
);
for my $test (@tests) {
my $src = $test->[0];
my $exp = $test->[1];
printf "-----|%s| ==> |%s|-----\n", $src, $exp;
for my $op (
[ 'ikegami 1' , sub { shift =~ /(.*)\.(.*)/s; } ]
, [ 'ikegami 2' , sub { split( /\.(?!.*\.\z)/s, shift) } ]
, [ 'rindex' , sub { my $p = rindex( $_[0], '.' );
( substr($_[0], 0, $p)
, substr($_[0], $p)
); }
]
) {
my ($head, $tail) = $op->[1]( $src );
my $res = join '|', ($head, $tail);
my $ok = $exp eq $res ? 'ok' : "fail: $exp expected.";
printf "%12s: %-20s => %-20s : %s\n", $op->[0], $src, $res, $ok;
}
}
выход:
-----|ab.cde.fg.hi| ==> |ab.cde.fg|.hi|-----
ikegami 1: ab.cde.fg.hi => ab.cde.fg|hi : fail: ab.cde.fg|.hi expected.
ikegami 2: ab.cde.fg.hi => ab|cde : fail: ab.cde.fg|.hi expected.
rindex: ab.cde.fg.hi => ab.cde.fg|.hi : ok
-----|abxcdexfg.hi| ==> |abxcdexfg|.hi|-----
ikegami 1: abxcdexfg.hi => abxcdexfg|hi : fail: abxcdexfg|.hi expected.
ikegami 2: abxcdexfg.hi => abxcdexfg|hi : fail: abxcdexfg|.hi expected.
rindex: abxcdexfg.hi => abxcdexfg|.hi : ok