mkHun mkHun - 5 months ago 44
Perl Question

\x not working inside the substitution

I'm trying to decode the unicode characters. So I simply tried the hexadecimal escape sequence

inside the regex substitution

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

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

#now $kv data is https://someurl/\u0026locale=en-GB\u0026mkhun=ccce


I'm trying substitute the all unicode character.

My expected output is:


Below mentioned
statement gives the expected output. However the regex seems doesn't working properly.

print "\x{0026}";


The problem with s/\\u(.{4})/"\x{$1}"/e is that the backslash escape \x{$1} is evaluated at compile time, which gives a NULL byte:

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

If we escape the backslash in front of x ( s/\\u(.{4})/"\\x{$1}"/ge ) we get a string with literal escape sequences, but still not the desired unicode character:

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

The output is now:


However, the following seems to work:

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

or alternatively, using a double ee:

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