$search_32bit = '(80 71 C3 (\S{8}) (77 55 66))';
$search_32bit =~ s/\s //g;
$replace_32bit = 'A0 B0 C0 \2\3';
$replace_32bit =~ s/\s //g;
@repls_32 = (
[ $search_32bit, $replace_32bit],
);
$hex = "9090908071C312345678775566000000777777";
foreach my $r (@repls_32) {
$hex_tmp = $hex;
(my $s_sign, my $mat_pos) = eval "\$hex =~ s/$r->[0]/$r->[1]/i;return (\$1, \$-[0])";
$len = length($s_sign);
$replaced_str = substr($hex, $mat_pos, $len);
print "matched_str: $s_sign\n";
print "mat_pos: $mat_pos\n";
print "length: $len\n";
print "replaced_str: $replaced_str\n";
}
輸出如下:
matched_str: 8071C312345678775566
mat_pos: 6
length: 20
replaced_str: A0B0C012345678775566
我的問題:
有沒有更好的方法來保存正則運算式的替換部分(即 $replaced_str: A0B0C012345678775566)?
uj5u.com熱心網友回復:
獲取替換字串的一種方法,大概是在正則運算式運行時動態構建的
my $repl_str;
$str =~ s{$pattern}{ $repl_str = code-to-build-replacement }e;
一個具體的例子:用句點填充捕獲
my $str = 'funny';
my $repl_str;
$str =~ s{ (.{3}) }{ $repl_str = '.' . $1 . '.' }ex;
say $repl_str; #--> .fun.
say $str; #--> .fun.ny
關鍵是替換部分內的賦值運算式也回傳(賦值的值),所以替換字串仍然可以用于正則運算式;這樣做不會破壞事情。現在還有另一個變數在浮動,但我認為替換字串沒有內置的正則運算式變數。
根據替換的不同,這并不總是通過添加該分配而起作用;回想一下,用/e替換邊是代碼。因此,.$1.上面在“正常”替換(沒有/e)中的內容必須調整為有效代碼,其中.字串連接到捕獲,然后我們可以添加賦值并運行它。
我希望你能適應它eval,我不知道你為什么需要它——除了運行從外部提供的代碼,作為一個字串?即便如此,我希望只提供正則運算式的一部分,您仍然可以撰寫正則運算式并且可以執行上述操作。
澄清問題中的一些要點會有所幫助,但希望這是有用的。(如果可能的話,將來考慮為您的需要構建一個簡單明了的示例。)
在這種特定情況下,存在一種復雜情況,其中那些\1(等)似乎是捕獲組。然后他們需要$1(等),并且需要更多的作業來使用一個變數
一種方法是從給定的$replace字串中構建實際的替換字串
use warnings;
use strict;
use feature 'say';
my $str = shift // '9090908071C312345678558765432166aaaabbbb7700abc' ;
say $str;
my $search = qr/8071C3(\S{8})55(\S{8})66(\S{8})77/;
my $replace = q(A0B0C0$155$266$377);
my $repl_str;
$str =~ s/$search/$repl_str = build_repl($replace, $1, $2, $3)/e;
# or use @{^CAPTURE} instead of $1,$2,$3 with perl-5.27.2 or newer
say $str;
say $repl_str;
sub build_repl {
my ($r, @captures) = @_;
my @terms = grep { $_ } split /(\$\d)/, $r;
# Terms that start with $ are $N's and are picked from input
my $str = join '', map { /^\$/ ? shift(@captures) : $_ } @terms;
return $str;
}
See a discussion below.? This prints
9090908071C312345678558765432166aaaabbbb7700abc 909090A0B0C012345678558765432166aaaabbbb7700abc A0B0C012345678558765432166aaaabbbb77
(This is correct, if hard to see. Why such long and hard-to-read example in a question?)
I use qr to build a regex pattern, but '' (or q()) works here as well. Since 5.27.2 there is a builtin @{^CAPTURE} variable so perhaps can use that instead of the explicit $1,$2,... list.
A similar example with more explanation can be seen in this post. There are other ways to organize code to build the replacement string of course, and there are useful libraries for that as well.
The sub above takes the captures and so has them on hand and can compose the replacement string. That /e is needed so to run the sub out of a regex, once the captures are available.
The linked answer builds code and uses /ee. Please read carefully about the warnings to that and follow links. I don't see that that is needed here.
? This is a discussion from comments (now deleted?) of whether the above can be simplified.
Any variables in the replacement side, including $N, are evaluated and concatenated with any literal strings into a replacement string.
my $str = 'bc';
$str =~ s/(.)/a$1/; #--> abc
But when the replacement-to-be is in a variable, it's different. With
$r = "a$1"; # Interpreter doesn't know what "$1" is
$str =~ s/(.)/a$1/; #--> Use of uninitialized value $1...
If we try with single quotes then that's what we get, mere literal characters a and $ and 1
$r = 'a$1'; # now it won't try to evaluate $1 ...
$str =~ s/(.)/a$1/; #--> a$1c (but it doesn't do it here either)
So how do we make it put $1 in the replacement side and see it as a variable and evaluate it?
That's the kind of a thing the /e modifier is for, where our variable now must contain valid code-to-be. Yet this is still not enough to get $1 evaluated; we need /ee for the whole job
$r = q('a'.$1);
$str =~ s/(.)/a$1/ee; #--> abc (that /ee is very unsafe)
But this got a bit complicated, and $r ($replace in the question) as given must be processed into valid code-to-be, and we are opening a huge security hole since /ee evaluates any string as code and in that string then evaluates code. (Follow links in a post linked in the text.)
Then why not just call a function to which we can pass $Ns, nicely evaluated so the function gets the actual captured strings. Then in the function we can parse $r and build our replacement string.
So what that build_repl is for is needed. (Which is why there are libraries for it.)
One way to try these things is with one-liners like
perl -wE'$_ = q(bc); $r = q(a$1); s/(.)/$r/; say'
and
perl -wE'$_ = q(bc); $r = q("a".$1); s/(.)/$r/ee; say'
That q(...) is the same as single-quotes while qq(...) is double quotes.
uj5u.com熱心網友回復:
首先,讓我們修復您的錯誤。
use String::Substitution qw( sub_modify );
my $search = qr/(80 71 C3 (\S{8}) (77 55 66))/xi;
my $replace = 'A0 B0 C0 $2$3' =~ s/\s //gr; # \1 should be $1 in replacement.
sub_modify($hex, $search, $replace); # Fix code injection bugs
這相當于
use String::Substitution qw( sub_modify interpolate_match_vars );
my $search = qr/(80 71 C3 (\S{8}) (77 55 66))/xi;
my $replace = 'A0 B0 C0 $2$3' =~ s/\s //gr;
sub_modify($hex, $search, sub { interpolate_match_vars($replace, @_) });
現在,這只是保存字串的問題。
use String::Substitution qw( sub_modify interpolate_match_vars );
my $search = qr/(80 71 C3 (\S{8}) (77 55 66))/xi;
my $replace = 'A0 B0 C0 $2$3' =~ s/\s //gr;
my $replacement_str;
sub_modify($hex, $search, sub { $replacement_str = interpolate_match_vars($replace, @_) });
不過,上面有一種非常由內而外的感覺。可以如下翻轉:
use String::Substitution qw( interpolate_match_vars last_match_vars );
my $search = qr/(80 71 C3 (\S{8}) (77 55 66))/xi;
my $replace = 'A0 B0 C0 $2$3' =~ s/\s //gr;
my $replacement_str;
if ($hex =~ $search) {
$replacement_str =
substr($hex, $-[0], $ [0] - $-[0]) =
interpolate_match_vars($replace, last_match_vars());
}
這也使保存原始匹配變得容易。
use String::Substitution qw( interpolate_match_vars last_match_vars );
my $search = qr/(80 71 C3 (\S{8}) (77 55 66))/xi;
my $replace = 'A0 B0 C0 $2$3' =~ s/\s //gr;
my $match_str;
my $replacement_str;
if ($hex =~ $search) {
my $match_ref = \substr($hex, $-[0], $ [0] - $-[0]);
$match_str = $$match_ref;
$replacement_str = interpolate_match_vars($replace, last_match_vars());
$$match_ref = $replacement_str;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/316795.html
