I have a string like "ab.cde.fg.hi", and I want to split it into two strings.
- "ab.cde.fg"
- ".hi"
How to do so? I got some开发者_开发百科 code written that will get me the 2nd string but how do I retrieve the remaining?
$mystring = "ab.cde.fg";
$mystring =~ m/.*(\..+)/;
print "$1\n";
my ($first, $second) = $string =~ /(.*)(\..*)/;
You can also use split
:
my ($first, $second) = split /(?=\.[^.]+$)/, $string;
Are you sure you aren’t looking for...
($name,$path,$suffix) = File::Basename::fileparse($fullname,@suffixlist);
my @parts = /(.*)\.(.*)/s;
my @parts = split /\.(?!.*\.)/s;
my @parts = split /\.(?=[^.]*\z)/s;
Update: I misread. The "." should be included in the second part, but it's not in the above. The above should be:
my @parts = /(.*)(\..*)/s;
my @parts = split /(?=\.(?!.*\.))/s;
my @parts = split /(?=\.[^.]*\z)/s;
To promote my idea to use rindex to get
1) "ab.cde.fg"
2) ".hi"
from "ab.cde.fg.hi"
, I wrote this script to make experiments easier:
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;
}
}
output:
-----|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
精彩评论