spiderbo has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

Searched high and low for this one without any joy, so came here for some advice.

I have a string where I would like to replace all the hex values to 'normal' chars.

This is my string: my $title = "C%3A%5CMy%20Documents%5CMy%20Pictures%5Cbodmin1.jpg"

I would like it to read: C:\My Documents\My Pictures\bodmin1.jpg

I tried this, but it didn't work: my $testtitle =~ s/%([0-9a-fA-F][0-9a-fA-F])/pack("c",hex($title))/ge;

Any suggestions or ideas?

Replies are listed 'Best First'.
Re: More regex's with hex's
by broquaint (Abbot) on Apr 22, 2003 at 11:11 UTC
    Why regex when you can CPAN :)
    use CGI::Lite print url_decode("C%3A%5CMy%20Documents%5CMy%20Pictures%5Cbodmin1.jpg" +); __output__ C:\My Documents\My Pictures\bodmin1.jpg
    See. CGI::Lite for more info.
    HTH

    _________
    broquaint

Re: More regex's with hex's
by Abigail-II (Bishop) on Apr 22, 2003 at 11:11 UTC
    Well, hex($title) will assume that C%3A%5CMy%20Documents%5CMy%20Pictures%5Cbodmin1.jpg is a hex number. What you want is
    s/%([0-9a-fA-F][0-9a-fA-F])/pack("c",hex($1))/ge;

    Of course, there are several modules, include URI::Escape module, doing the work for you.

    Abigail

Re: More regex's with hex's
by davorg (Chancellor) on Apr 22, 2003 at 11:16 UTC

    You probably meant

    $title =~ s/%([0-9a-fA-F][0-9a-fA-F])/pack("c",hex($1))/ge;

    But you'd almost certainly be better off with the uri_unescape function from URI::Escape.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      Many thanks, worked a charm without having to install additional modules.