Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

x, sprintf and s///e

WeirdofreakWeirdofreak Posts: 439Member
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 [italic]"x" . sprintf "%x", $1[/italic], 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?

Comments

  • JonathanJonathan Posts: 2,914Member
    : 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 [italic]"x" . sprintf "%x", $1[/italic]...
    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.");

  • WeirdofreakWeirdofreak Posts: 439Member
    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.
  • JonathanJonathan Posts: 2,914Member
    : 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.");

Sign In or Register to comment.