www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Decoding HTML escape sequences

reply Hugo Florentino via Digitalmars-d-learn writes:
Hi, I have some documents where some strings appears in HTML escape 
sequences in one of these forms:



And I would like to recode them to readable form:

<SCRIPT LANGUAGE="Javascript">

I tried something like this, using regular expressions and the uri 

import std.stdio, std.file, std.encoding, std.string, std.regex, 

static auto re = regex(`(%[a-fA-F0-9]{2})`);

int main(in string[] args)
   if (args.length < 2)
     writeln("Usage: unescape file1.htm > file2.htm");
     return -1;
   auto input = cast(Latin1String) read(args[1]);
   string buffer;
   transcode(input, buffer);

   string output;
   foreach(m; matchAll(buffer, re)) output ~= decode(m.hit);


   return 0;

Unfortunately it doesn't seem to work 100%.

I would appreciate any suggestion.

Regards, Hugo
May 12 2014
parent "Adam D. Ruppe" <destructionator gmail.com> writes:
You should use decodeComponent instead of decode in your matchAll 

IMO encodeComponent and decodeComponent are the only two useful 
uri encode functions (btw same in JS, use decodeURIComponent 
instead of the other functions). The other ones have weird rules.
May 12 2014