Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6374633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:30:45+00:00 2026-05-25T01:30:45+00:00

This question is similar to the Remove html character entities in a string question

  • 0

This question is similar to the Remove html character entities in a string question asked earlier on Stack Overflow. The accepted answer, however, does not address the issue of named HTML entities, e.g. ä for the character ä; It therefore cannot unescape all HTML.

I have some legacy HTML which uses named HTML entities for non-ASCII characters. That is, ö instead of ö, ä instead of ä and so on. A full list of all named HTML entities is available on Wikipedia.

I’d like to unescape these HTML entities into their character equivalents, in a quick and efficient manner.


I have the code to do this in Python 3, using regular expressions:

import re
import html.entities

s = re.sub(r'&(\w+?);', lambda m: chr(html.entities.name2codepoint[m.group(1)]), s)

Regular expressions, however, don’t seem very popular, fast or easy to use in Haskell.


Text.HTML.TagSoup.Entity (tagsoup) has a useful table and functions for mapping named entities tpo codepoints. Using this, and the regex-tdfa package, I’ve fashioned an extremely slow equivalent in Haskell:

{-# LANGUAGE OverloadedStrings #-}
import Data.ByteString.Lazy.Char8 as L
import Data.ByteString.Lazy.UTF8 as UTF8
import Text.HTML.TagSoup.Entity (lookupEntity)
import Text.Regex.TDFA ((=~~))

unescapeEntites :: L.ByteString -> L.ByteString
unescapeEntites = regexReplaceBy "&#?[[:alnum:]]+;" $ lookupMatch
 where
  lookupMatch m =
    case lookupEntity (L.unpack . L.tail . L.init $ m) of
      Nothing -> m
      Just x -> UTF8.fromString [x]

-- regex replace taken from http://mutelight.org/articles/generating-a-permalink-slug-in-haskell
regexReplaceBy :: L.ByteString -> (L.ByteString -> L.ByteString) -> L.ByteString -> L.ByteString
regexReplaceBy regex f text = go text []
 where
  go str res =
    if L.null str
      then L.concat . reverse $ res
      else
        case (str =~~ regex) :: Maybe (L.ByteString, L.ByteString, L.ByteString) of
          Nothing -> L.concat . reverse $ (str : res)
          Just (bef, match , aft) -> go aft (f match : bef : res)

The unescapeEntities function runs several orders of magnitude slower than the Python version above. The Python code can convert about 130 MB in 7 seconds, whereas my Haskell version runs for several minutes.

I’m looking for a better solution, primarily in terms of speed. But I’d also like to avoid regular expressions, if possible (speed and avoiding regular expressions seem to go hand in hand in Haskell anyway).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T01:30:45+00:00Added an answer on May 25, 2026 at 1:30 am

    Here’s my version. It uses String (instead of ByteString).

    import Text.HTML.TagSoup.Entity (lookupEntity)
    
    unescapeEntities :: String -> String
    unescapeEntities [] = []
    unescapeEntities ('&':xs) = 
      let (b, a) = break (== ';') xs in
      case (lookupEntity b, a) of
        (Just c, ';':as) ->  c  : unescapeEntities as    
        _                -> '&' : unescapeEntities xs
    unescapeEntities (x:xs) = x : unescapeEntities xs
    

    I would guess it’s faster because it doesn’t use the expensive regex operations. I haven’t tested it. You could adapt it for ByteString or for Data.Text if you need it faster.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is similar to GWT Table that supports sorting, scrolling and filtering However
This question seems similar to those asked in a few other posts: here and
This question is similar to another one I asked , but whereas in that
Ok, I have previously asked a similar question to this but it was voted
I asked a somewhat similar question this afternoon and then realized as great as
I asked a question similar to this one here and was given great answers,
First of all, I know I asked a similar question before but this one
Helo Experts again. I have asked a question similar to this in the past.
This question is similar to this one How do I add options to a
This question is similar to Getting Emacs fill-paragraph to play nice with javadoc-like comments

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.