我正在尝试提取字符:328c1e460b000a2020202020200000
来自字符串:01e000000fd00328c1e460b000a202020202020000000fc00434f4
我在Windows XP上使用Perl并尝试过:
$trailer =~/00fd00.*00fc00/;
$trailer =~/00fd00\w+00fc00/;
例如:
$trailer ="01e000000fd00328c1e460b000a202020202020000000fc00434f4";
print"Original $trailer\n";
#$trailer =~/00fd00.*00fc00/;
$trailer =~/00fd00\w+00fc00/;
print "Final $trailer\n";
输出结果为:
Original 01e000000fd00328c1e460b000a202020202020000000fc00434f4
Final 01e000000fd00328c1e460b000a202020202020000000fc00434f4
答案 0 :(得分:5)
您可以执行以下操作:
$trailer =~ s/.*00fd00(.*)00fc00.*/$1/;
普通匹配不会改变变量本身,需要替换。
答案 1 :(得分:4)
您可以使用regex captures: -
my $trailer ="01e000000fd00328c1e460b000a202020202020000000fc00434f4";
my ($trailer_extract) = $trailer =~/00fd00(\w+)00fc00/;
print "Original $trailer\n";
print "Final $trailer_extract\n";
如果您仍然需要在代码中的其他位置引用未提取的字符串,那么这有利于保留$ trailer的完整替换。