HaskellでPosix正規表現を使う

前提

stack --version
# Version 1.6.5 x86_64 hpack-0.20.0

インストールする

stack install regex-posix
# regex-base-0.93.2: download
# regex-base-0.93.2: configure
# regex-base-0.93.2: build
# regex-base-0.93.2: copy/register
# regex-posix-0.95.2: download
# regex-posix-0.95.2: configure
# regex-posix-0.95.2: build
# regex-posix-0.95.2: copy/register
# Completed 2 action(s).

使う

import Text.Regex.Posix

main :: IO ()
main = do
    let matches = "Text of Hello, World!" =~ "^Text of (.+)$" :: [[String]]
    putStrLn $ case matches of
        [] -> "Nothing"
        _ -> matches !! 0 !! 1

出力

stack runghc helloworld.hs 
# Hello, World!