regex-wrapper: Types that can only be constructed if they match a regular expression

[ bsd3, data, library ] [ Propose Tags ]

Provides tooling for working with types whose values must match a regular expression provided in the type.

[Skip to Readme]


[Index] [Quick Jump]


Note: This package has metadata revisions in the cabal description newer than included in the tarball. To unpack the package including the revisions, use 'cabal get'.

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees


Versions [RSS], (info)
Change log
Dependencies aeson, base (>=4.12 && <5.0), bytestring, containers, hashable, regex-tdfa, string-conv, text [details]
License BSD-3-Clause
Copyright (c) 2019 Luke Clifton
Author Luke Clifton
Revised Revision 1 made by lukec at 2020-12-04T12:15:58Z
Category Data
Bug tracker
Source repo head: git clone
Uploaded by lukec at 2019-11-11T06:39:13Z
Distributions NixOS:
Downloads 586 total (25 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2019-11-11 [all 1 reports]

Readme for regex-wrapper-

[back to package description]


This library allows you to create types that are guaranteed to contain a string that matches a given regular expression which is expressed at the type level.

newtype User = User (Matched String "^[a-zA-Z0-9]{4,15}$")

parseUser :: String -> Either (RegexError String) User
parseUser = fmap User . parseMatchedEither

prettyUser :: User -> String
prettyUser (User m) = asString m

main :: IO ()
main = do
    l <- getLine
    case parseUser l of
        Right user -> putStrLn $ "Hello, " ++ prettyUser user ++ "!"
        Left error -> putStrLn $ "Could not parse username: " ++ prettyRegexError  error
Could not parse username: The input "bad" did not match the pattern ^[a-zA-Z0-9]{4,15}$
Hello, good!