Next 3.6 Regular expression example Prev

Regular expression example

def unescapeHTML(string)
   str = string.dup
   str.gsub!(/&(.*?);/) {
      case $1
      when /\Aamp\z/i           then '&'
      when /\Aquot\z/i          then '"'
      when /\Agt\z/i            then '>'
      when /\Alt\z/i            then '<'
      when /\A#(\d+)\z/         then Integer($1).chr
      when /\A#x([0-9a-f]+)\z/i then $1.hex.chr
      end
   }
   str
end

p unescapeHTML("1&lt;2 &amp;&amp; 4&gt;3")       # 1<2 && 4>3
p unescapeHTML("&quot;A&quot; = &#65; = &#x41;") # "A" = A = A

Next Ruby for Perl programmers Prev