Perl

Moderators: Jonathan
Number of threads: 1259
Number of posts: 3644

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
\x, sprintf and s///e Posted by Weirdofreak on 19 Dec 2003 at 3:06 PM
I'm trying to convert numerical HTML escape sequences to the actual character, such as ' to a single quote. I can get the number and convert it to hexadecimal, but I can't work out how to append it to a \x so that it makes a single string. This is the regex I'm using:

s/&#(\d{2,3});//ge

The problem, as you probably guessed lies in the substitution part. The most obvious answer is "\x" . sprintf "%x", $1, but that just produces a space followed by the hex number. I also tried eval-ing it, which didn't change anything, and using a reference trick - @{[sprintf "%x", $1]} to get the hex number inside the quotes, which told me that @ was not a valid thingie to go after \x, and putting it in completely another string - "\x"."@{[sprintf \"%x\", $1]}" - which gives the same as the eval and obvious thing.

Is there something obvious I'm missing here, or is it incredibly complex? Is it even possible?
Report
Re: \x, sprintf and s///e Posted by Jonathan on 19 Dec 2003 at 4:07 PM
: I'm trying to convert numerical HTML escape sequences to the actual
: character, such as ' to a single quote. I can get the number and
: convert it to hexadecimal, but I can't work out how to append it to
: a \x so that it makes a single string. This is the regex I'm using:
:
: s/&#(\d{2,3});//ge
:
: The problem, as you probably guessed lies in the substitution part.
: The most obvious answer is "\x" . sprintf "%x", $1...
Unless I'm missing something, I beg to differ. I believe the most obvious answer would be:-

s/&#(\d{2,3};/chr($1)/ge;

Should you maybe match \d{1,3} instead, BTW?

Jonathan

###
for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
(tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
/(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

Report
Re: \x, sprintf and s///e Posted by Weirdofreak on 20 Dec 2003 at 1:36 AM
Yep, that works fine. Thanks.

For whatever reason, it starts at 00, 01, etc., but doesn't put a zero in front of 2 digit numbers, so {2,3} works fine.
Report
Re: \x, sprintf and s///e Posted by Jonathan on 20 Dec 2003 at 8:17 AM
: Yep, that works fine. Thanks.
No probs.

: For whatever reason, it starts at 00, 01, etc., but doesn't put a
: zero in front of 2 digit numbers, so {2,3} works fine.
OK, thanks for the enlightenment.

Jonathan



###
for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
(tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
/(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.