\ x does not work inside replacement

I am trying to decode Unicode characters. So I just tried the hexadecimal escape sequence \x{}inside the regex lookupe

use LWP::Simple;
my $k = get("url");

my ($kv) =map{/js_call\(\\"(.+?)\\"\)/} $k;

#now $kv data is https://someurl/call.pl?id=15967737\u0026locale=en-GB\u0026mkhun=ccce

$kv=~s/\\u(.{4})/"\x{$1}"/eg;

I am trying to replace all unicode characters.

My expected result:

https://someurl/call.pl?id=15967737&locale=en-GB&mkhun=ccce

The following statement printgives the expected result. However, regex does not work properly.

print "\x{0026}";
+4
source share
3 answers

The problem with s/\\u(.{4})/"\x{$1}"/eis that the backslash escape \x{$1}is evaluated at compile time, giving NULL bytes:

$ perl -E 'printf "%vX\n", "\x{$1}"'
0

x (s/\\u(.{4})/"\\x{$1}"/ge), escape-, :

use feature qw(say);
$kv = '\u0026';
$kv =~ s/\\u(.{4})/"\\x{$1}"/ge;
say $kv; 

:

\x{0026}

"\x{0026}" , Perl, . eval(EXPR).

$kv =~ s/\\u(.{4})/ my $s = eval(qq{"\\x{$1}"}); die $@ if $@; $s /ge;

$kv =~ s/\\u(.{4})/ qq{"\\x{$1}"} /gee;

, :

$kv =~ s/\\u(.{4})/chr hex $1/ge;
+7

use warnings, , $1 , .

$kv =~ s/\\u(.{4})/ sprintf("\"\\x{%s}\"", $1) /eeg;

, . , , , " " $"".

+2

Perhaps you can also try:

$kv=~s/\\u([[:xdigit:]]{1,5})/chr(eval("0x$1"))/egis;

Thank.

+2
source

Source: https://habr.com/ru/post/1657466/


All Articles