Skip to content

Instantly share code, notes, and snippets.

@ryo1kato
Created August 28, 2014 14:12
Show Gist options
  • Select an option

  • Save ryo1kato/d24f717024c5256b80b1 to your computer and use it in GitHub Desktop.

Select an option

Save ryo1kato/d24f717024c5256b80b1 to your computer and use it in GitHub Desktop.
import Control.Monad
import Data.Maybe
regexChars = "^$(|)[]{}.*"
regexCharsLast = "$)]}.*"
toPlainString' :: String -> String -> Maybe String
toPlainString' s [] = Just s
toPlainString' s (r:[]) = if r `elem` regexCharsLast
then Nothing
else Just (r:s)
toPlainString' s (r1:r2:rs) = if r1 `elem` regexChars
then Nothing
else if r1 == '\\' && r2 `elem` ('\\':regexChars)
then toPlainString' (r2:s) rs
else toPlainString' (r1:s) (r2:rs)
toPlainString str = liftM reverse $ toPlainString' [] str
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment