Using regular expressions

Haskell:
Using regular expressions

How to:

In Haskell, regex functionalities are not part of the standard library, necessitating the use of third-party packages like regex-base along with a compatible backend like regex-posix (for POSIX regex support), regex-pcre (for Perl-compatible regex), etc. Here’s how you can use these packages to work with regular expressions.

First, ensure you have the packages installed by adding regex-posix or regex-pcre to your project’s .cabal file or installing via cabal directly:

cabal install regex-posix

or

cabal install regex-pcre

Using regex-posix:

import Text.Regex.Posix ((=~))

-- Check if a string matches a pattern
isMatch :: String -> String -> Bool
isMatch text pattern = text =~ pattern :: Bool

-- Find the first match
findFirst :: String -> String -> String
findFirst text pattern = text =~ pattern :: String

main :: IO ()
main = do
    print $ isMatch "hello world" "wo"
    -- Output: True
    print $ findFirst "good morning, good night" "good"
    -- Output: "good"

Using regex-pcre:

import Text.Regex.PCRE ((=~))

-- Find all matches
findAll :: String -> String -> [String]
findAll text pattern = text =~ pattern :: [String]

main :: IO ()
main = do
    print $ findAll "test1 test2 test3" "\\btest[0-9]\\b"
    -- Output: ["test1","test2","test3"]

Each library has its particularities, but the general methodology of using =~ to apply the regex remains consistent, whether checking for a match or extracting substrings. Choosing between regex-posix or regex-pcre largely depends on your project’s needs and the specific regex capabilities required.