pcre2

Regular expressions via the PCRE2 C library (included)

https://github.com/sjshuck/hs-pcre2#readme

Version on this page:1.1.5
LTS Haskell 22.43:2.2.1
Stackage Nightly 2024-12-05:2.2.1
Latest on Hackage:2.2.1

See all snapshots pcre2 appears in

Apache-2.0 licensed by Shlomo Shuck and contributors
Maintained by [email protected]
This version can be pinned in stack with:pcre2-1.1.5@sha256:6d5b8eb9997794e9b1e3af3ce43a1d328a735d0d99f63e9b293d440db5747b70,6107

Module documentation for 1.1.5

pcre2

CI Hackage

Regular expressions for Haskell.

Teasers

embeddedLicensePlate :: Text -> Maybe Text
embeddedLicensePlate = match "[A-Z]{3}[0-9]{3,4}"
case "The quick brown fox" of
    [regex|brown\s+(?<animal>\w+)|] -> Text.putStrLn animal
    _                               -> error "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"

Features

  • Quiet functions with simple types—for the most part it’s
    Text (pattern) -> Text (subject) -> result.
  • Use partial application to create performant, compile-once-match-many code.
  • Low cognitive overhead—there’s just one custom datatype for both compile and match options, the Option monoid.
  • Text everywhere for interop with both C and the broader Haskell ecosystem.
  • Match failures expressed via Alternative or pattern match failures.
  • 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!
  • Few dependencies.
  • Bundled, statically-linked UTF-16 build of up-to-date PCRE2 (version 10.36), with a complete, exposed Haskell binding.

Wishlist

  • Many performance optimizations. Currently we are as much as 2–3× slower than other libraries for some operations, although things are improving. (We are already faster than regex-base/regex-pcre when working with Text.) 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.
  • Make use of DFA and JIT compilation.
  • Improve PCRE2 C compile time.
  • Add splitting support.

License

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

Main Author

©2020 Shlomo Shuck

Changes

Changelog and Acknowledgements

1.1.5

  • Fixed #17, where functions returning Alternative containers were not restricted to single results despite their documentation.
  • Minor improvements to docs and examples.

1.1.4

  • Fixed some incorrect foreign imports’ safety.

1.1.3.1

  • Fixed a very minor issue where pcreVersion still reported “10.35” even though it in fact was using 10.36.

1.1.3

  • Made in-house streaming abstraction based on streaming and removed the latter as a dependency.
  • Updated PCRE2 to 10.36 (no API changes).
  • Docs fixes.

1.1.2

  • Refactored using the streaming library. Fixed #11, where large global matches were very slow.

1.1.1

  • Fixed #12, where some functions returned too many match results.

1.1.0

  • Added global matching.
    • New functions matchAll, matchAllOpt, capturesAll, capturesAllOpt.
    • Changed all traversals from affine to non-affine.
  • Changed capturesOptA to capturesAOpt for naming consistency.

1.0.2

  • Fixed #4, where multiple named captures were not type-indexed correctly.
  • Established automated builds using Github Workflows. Thanks amesgen!

1.0.1.1

  • Temporarily eliminate all dependency version bounds to get it building on Hackage.

1.0.1

  • Fixed #1, where building on Windows would succeed but not run. Thanks Andrew!
  • Try to adjust dependency version bounds to get it building on Hackage. Thanks snoyberg!

1.0.0

  • Initial release.