BSD-3-Clause licensed by Soumik Sarkar
Maintained by [email protected]
This version can be pinned in stack with:parser-regex-0.3.1.0@sha256:1748a12bc76580a8f65830f6934405f697d6256bd2f31032ec3b6fe2f33a65e5,2636

parser-regex

Hackage Haskell-CI

Regex based parsers

Features

  • Parsers based on regular expressions, capable of parsing regular languages. Note that there are no extra features to make parsing non-regular languages possible.
  • Regexes are composed using combinators.
  • Resumable parsing of sequences of any type containing values of any type.
  • Special support for Text and String in the form of convenient combinators and operations like find and replace.
  • Parsing runtime is linear in the length of the sequence being parsed. No exponential backtracking.

Examples

Versus regex patterns

^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?

Can you guess what this matches?

This is a non-validating regex to extract parts of a URI, from RFC 3986. It can be translated as follows.

{-# LANGUAGE OverloadedStrings #-}
import Control.Applicative (optional)
import Data.Text (Text)

import Regex.Text (REText)
import qualified Regex.Text as R
import qualified Data.CharSet as CS

data URI = URI
  { scheme    :: Maybe Text
  , authority :: Maybe Text
  , path      :: Text
  , query     :: Maybe Text
  , fragment  :: Maybe Text
  } deriving Show

uriRE :: REText URI
uriRE = URI
  <$> optional (R.someTextOf (CS.not ":/?#") <* R.char ':')
  <*> optional (R.text "//" *> R.manyTextOf (CS.not "/?#"))
  <*> R.manyTextOf (CS.not "?#")
  <*> optional (R.char '?' *> R.manyTextOf (CS.not "#"))
  <*> optional (R.char '#' *> R.manyText)

It is now much easier to follow, and is composed out of familiar Applicative and Alternative combinators.

>>> R.reParse uriRE "https://github.com/meooow25/parser-regex?tab=readme-ov-file#parser-regex"
Just (URI { scheme = Just "https"
          , authority = Just "github.com"
          , path = "/meooow25/parser-regex"
          , query = Just "tab=readme-ov-file"
          , fragment = Just "parser-regex" })

Notice that it parses into a URI directly. Unlike with regex patterns, parsing is not a two-step process of matching successfully and then building the result out of “submatches”.

More parsing

Parsing is straightforward, even in cases where it would be impractical to try regex patterns and submatch extraction.

import Control.Applicative ((<|>))
import Data.Text (Text)

import Regex.Text (REText)
import qualified Regex.Text as R
import qualified Data.CharSet as CS

data Expr
  = Var Text
  | Expr :+ Expr
  | Expr :- Expr
  | Expr :* Expr
  deriving Show

exprRE :: REText Expr
exprRE = var `R.chainl1` mul `R.chainl1` (add <|> sub)
  where
    var = Var <$> R.someTextOf CS.asciiLower
    add = (:+) <$ R.char '+'
    sub = (:-) <$ R.char '-'
    mul = (:*) <$ R.char '*'
>>> import qualified Regex.Text as R
>>> R.reParse exprRE "a+b-c*d*e+f"
Just (((Var "a" :+ Var "b") :- ((Var "c" :* Var "d") :* Var "e")) :+ Var "f")

Find and replace

Find and replace using regexes are supported for Text and lists.

>>> import Control.Applicative ((<|>))
>>> import qualified Data.Text as T
>>> import qualified Regex.Text as R
>>>
>>> data Color = Blue | Orange deriving Show
>>> let re = Blue <$ R.text "blue" <|> Orange <$ R.text "orange"
>>> R.find re "color: orange"
Just Orange
>>>
>>> let re = T.toUpper <$> (R.text "cat" <|> R.text "dog" <|> R.text "fish")
>>> R.replaceAll re "locate selfish hotdog"
"loCATe selFISH hotDOG"

Notice that the replacement text is not fixed, it can be a transformation of the matched result.

Parse any sequence

Parsing is not restricted to text. One can parse a vector, a conduit, or any other sequence one might have.

import qualified Regex.Base as R
import qualified Data.Vector.Generic as VG -- from vector
import qualified Conduit as C -- from conduit

parseVector :: VG.Vector v c => R.Parser c a -> v c -> Maybe a
parseVector = R.parseFoldr VG.foldr

parseConduit :: Monad m => R.Parser c a -> C.ConduitT c x m (Maybe a)
parseConduit p = R.parseNext p C.await <* C.sinkNull
>>> import Control.Applicative (many)
>>> import qualified Regex.Base as R
>>> :{
let evenOddP :: R.Parser Int [(Int, Int)]
    evenOddP = R.compile $ many ((,) <$> R.satisfy even <*> R.satisfy odd)
:}
>>>
>>> import qualified Data.Vector as V
>>> parseVector evenOddP (V.fromList [6,1,2,5,4,3])
Just [(6,1),(2,5),(4,3)]
>>> parseVector evenOddP (V.fromList [4,3,1,2])
Nothing
>>>
>>> import Conduit ((.|))
>>> import qualified Conduit as C
>>> C.runConduit $ C.yieldMany [0..3] .| C.iterMC print .| parseConduit evenOddP
0
1
2
3
Just [(0,1),(2,3)]

Documentation

Documentation is available on Hackage: parser-regex

Already familiar with regex patterns? See the Regex pattern cheat sheet.

Alternatives

Other regex libraries

For a comparison of regex libraries, including benchmarks, see here.

regex-applicative

regex-applicative is the primary inspiration for this library, and is similar in many ways.

parser-regex attempts to be a more efficient and featureful library built on the ideas of regex-applicative, though it does not aim to provide a superset of regex-applicative’s API.

Regex pattern libraries

One of these libraries can be a better choice when the terseness or the established format of regex patterns is well-suited for the use case, e.g. for a user-supplied search string or a CLI argument.

regex-pcre, regex-pcre-builtin, pcre-light, pcre-heavy are also faster than parser-regex for typical use cases, but they do not have Unicode support and come with a risk of ReDoS.

Consider parser-regex instead if

  • You prefer parser combinators over regex patterns
  • You need more powerful parsing capabilities than just submatch extraction
  • You need to parse a sequence that is not supported by the above libraries

Non-regular parsing

If you are not restricted to regular parsing, there are many other parsing libraries you may use, too many to list here. See the “Parsing” category on Hackage for a start.

Contributing

Questions, bug reports, documentation improvements, code contributions welcome! Please open an issue as the first step.

Changes

0.3.1.0 – 2026-12-06

  • Support for creating CharSets using the QualifiedStrings extension coming in GHC 10.0
  • Update case mappings to Unicode 18.0.0
  • Documentation improvements

0.3.0.0 – 2025-04-19

  • Breaking changes—Internal modules only
    • Changes to internal representations
  • Performance improvements
    • Fix a pessimization due to boxity analysis on GHC >= 9.4. Improves parsing time by up to 23%.

0.2.0.2 – 2025-03-15

  • Compatibility with MicroHs
  • Performance improvements
    • Avoid some closure allocations, improving parsing time by up to 10%.
  • Fix compileBounded’s behavior on negative bounds

0.2.0.1 – 2024-12-25

  • Documentation improvements

0.2.0.0 – 2024-11-24

  • Breaking changes
    • Parsing fails more eagerly. This affects lazy list parsing and parsing via the Regex.Base functions prepareParser and stepParser.
  • Additions
    • Added Regex.Base.parseNext.
  • Other
    • Some internal modules are now exported.

0.1.0.0 – 2024-03-04

  • First version.