pcre2: Regular expressions via the PCRE2 C library (included)

[ apache, library, text ] [ Propose Tags ] [ Report a vulnerability ]

Please see the README on GitHub at https://github.com/sjshuck/hs-pcre2


[Skip to Readme]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 1.0.0, 1.0.1, 1.0.1.1, 1.0.2, 1.1.0, 1.1.1, 1.1.2, 1.1.3, 1.1.3.1, 1.1.4, 1.1.5, 2.0.0, 2.0.1, 2.0.2, 2.0.3, 2.0.4, 2.0.5, 2.1.0, 2.1.0.1, 2.1.1, 2.1.1.1, 2.2.0, 2.2.1, 2.2.2
Change log ChangeLog.md
Dependencies base (>=4.9 && <5), containers, microlens, mtl, template-haskell, text (>=2) [details]
License Apache-2.0
Copyright 2020-2025 Steven Shuck
Author Steven Shuck and contributors
Maintainer stevenjshuck@gmail.com
Category Text
Home page https://github.com/sjshuck/hs-pcre2#readme
Bug tracker https://github.com/sjshuck/hs-pcre2/issues
Source repo head: git clone https://github.com/sjshuck/hs-pcre2
Uploaded by SShuck at 2025-01-06T02:43:53Z
Distributions LTSHaskell:2.2.1, NixOS:2.2.1, Stackage:2.2.1
Reverse Dependencies 1 direct, 0 indirect [details]
Downloads 4272 total (55 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2025-01-06 [all 1 reports]

Readme for pcre2-2.2.2

[back to package description]

pcre2

CI Hackage

Regular expressions for Haskell.

Teasers

licensePlate :: Text -> Maybe Text
licensePlate = match "[A-Z]{3}[0-9]{3,4}"

licensePlates :: Text -> [Text]
licensePlates = match "[A-Z]{3}[0-9]{3,4}"
case "The quick brown fox" of
    [regex|\bbrown\s+(?<animal>[A-z]+)\b|] -> Text.putStrLn animal
    _                                      -> die "nothing brown"
let kv'd = lined . packed . [_regex|(?x)  # Extended PCRE2 syntax
        ^\s*          # Ignore leading whitespace
        ([^=:\s].*?)  # Capture the non-empty key
        \s*           # Ignore trailing whitespace
        [=:]          # Separator
        \s*           # Ignore leading whitespace
        (.*?)         # Capture the possibly-empty value
        \s*$          # Ignore trailing whitespace
    |]

forMOf kv'd file $ execStateT $ do
    k <- gets $ capture @1
    v <- gets $ capture @2
    liftIO $ Text.putStrLn $ "found " <> k <> " set to " <> v

    case myMap ^. at k of
        Just v' | v /= v' -> do
            liftIO $ Text.putStrLn $ "setting " <> k <> " to " <> v'
            _capture @2 .= v'
        _ -> liftIO $ Text.putStrLn $ "no change for " <> k

Features

  • No opaque "Regex" object. Instead, quiet functions with simple types—for the most part it's Text (pattern) -> Text (subject) -> result.
  • No custom typeclasses.
  • A single datatype for both compile and match options, the Option monoid.
  • UTF-8 Text everywhere.
  • Match success expressed via Alternative.
  • Opt-in Template Haskell facilities for compile-time verification of patterns, indexing captures, and memoizing inline regexes.
  • Opt-in lens support.
  • No failure monads to express compile errors, preferring pure functions and throwing imprecise exceptions with pretty Show instances. Write simple code and debug it. Or, don't, and use the Template Haskell features instead. Both are first-class.
  • Vast presentation of PCRE2 functionality. We can even register Haskell callbacks to run during matching!
  • Zero-copying of substrings where beneficial.
  • Few dependencies.
  • Bundled, statically-linked build of up-to-date PCRE2 (version 10.44), with a complete, exposed Haskell binding.

Performance

Currently we are slower than other libraries. For example:

Operation pcre2 pcre-light regex-pcre-builtin
Compile and match a regex 3.9 μs 1.2 μs 2.9 μs

If it's really regex processing that's causing a bottleneck, pcre-light/-heavy/lens-regex-pcre are recommended instead of this library for the very best performance.

Wishlist

  • Many performance optimizations.
  • Make use of DFA matching for lazy (infinite) inputs. This likely requires some upstream changes as well but in theory it's possible
  • Improve compile time. Support external libpcre2 maybe

License

Apache 2.0. PCRE2 is distributed under the 3-clause BSD license.

Main Author

©2020–2025 Steven Shuck