Regex 字符串匹配与提取
我有一个类似“ab.cde.fg.hi”的字符串,我想把它分成两个字符串Regex 字符串匹配与提取,regex,perl,string-split,Regex,Perl,String Split,我有一个类似“ab.cde.fg.hi”的字符串,我想把它分成两个字符串 “ab.cde.fg” “嗨” 如何做到这一点?我写了一些代码,可以得到第二个字符串,但是我如何检索剩余的呢 $mystring = "ab.cde.fg"; $mystring =~ m/.*(\..+)/; print "$1\n"; 您还可以使用拆分: my ($first, $second) = split /(?=\.[^.]+$)/, $string; 你确定你不是在找 ($name,$path,$suff
$mystring = "ab.cde.fg";
$mystring =~ m/.*(\..+)/;
print "$1\n";
您还可以使用拆分
:
my ($first, $second) = split /(?=\.[^.]+$)/, $string;
你确定你不是在找
($name,$path,$suffix) = File::Basename::fileparse($fullname,@suffixlist);
更新:我读错了。“.”应该包含在第二部分中,但不在上面。以上内容应为:
my @parts = /(.*)(\..*)/s;
my @parts = split /(?=\.(?!.*\.))/s;
my @parts = split /(?=\.[^.]*\z)/s;
为了推广我的想法,使用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
应该提到的是,最后两个拆分了
$\ucode>string@eugeney,所有6个变量都在$\uuu
上运行,尽管所有6个变量都可以轻松调整以在不同的变量上工作。
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