MyNixOS website logo
Description

Regular expressions via the PCRE2 C library (included)

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

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
    _                                      -> 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

  • No opaque "Regex" object. Instead, 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.
  • No custom typeclasses.
  • A single datatype for both compile and match options, the Option monoid.
  • Text everywhere. See below for a version guide.
  • 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 UTF-8 (formerly UTF-16) build of up-to-date PCRE2 (version 10.40), 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.

Unicode

| Encoding | text version | pcre2 version | Code unit representation | | :-- | :-- | :-- | :-- | | UTF-8 | ≥ 2 | ≥ 2.2 | Foreign.C.Types.CUChar | | UTF-16 | < 2 | < 2.2 | Foreign.C.Types.CUShort |

Wishlist

  • Many performance optimizations. Currently we are slower than other libraries. For example:
  • Make use of DFA matching 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–2022 Shlomo Shuck.

Metadata

Version

2.2.1

License

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows