How to simplify Regex with Data.Text? This function tells that elements of content either (maybe) match regexes that you like or match regexes that you don't like.

This messy code requires Data.Text.Lazy but internally shuffles around internal and strict versions. Also list Monad code looks fishy to me.

How can it be made simpler, faster, more general (especially work with any Text and String)?

{-# LANGUAGE OverloadedStrings #-}

module Search (analyze) where

import qualified Data.Text.Lazy as T
import Data.Maybe()
import Text.Regex.TDFA
import Text.Regex.Base.Context()
import Text.Regex.Base.RegexLike()
import Text.Regex.TDFA.Text
import Data.Function (on)
import Data.Maybe (maybeToList)

matches :: [T.Text] -&gt; T.Text -&gt; [T.Text]
matches patterns content = map (T.fromStrict) $ concat $ do
  pattern &lt;- patterns
  return $ maybeToList $ (on (=~~) T.toStrict) content pattern

analyze :: [T.Text] -&gt; [T.Text] -&gt; T.Text -&gt; Either [T.Text] [T.Text]
analyze likes dislikes text = 
    case matches likes text of
      liked@(_:_) -&gt; 
        case matches dislikes text of
             [] -&gt; Right liked
             disliked@(_:_) -&gt; Left disliked
      _ -&gt; Left []
To fix lazy/strict shuffling you need to import Text.Regex.TDFA.Text.Lazy module. It contains TDFA-related instances for Data.Text.Lazy

Some other things to consider.

You can skip maybeToList because (=~) returns m target for some monad m.

- return $ maybeToList $ (on (=~~) T.toStrict) content pattern
+ return $ (on (=~~) T.toStrict) content pattern


This will work without any further changes because (=~) will select list monad and match failure will be represented by empty list.

Do-notation for list monad can be replaced with single map and concat . map f -- with concatMap

- matches patterns content = map (T.fromStrict) $ concat $ do
-   pattern &lt;- patterns
-   return $ maybeToList $ (on (=~~) T.toStrict) content pattern
+ matches patterns content = map T.fromStrict
+   $ concatMap ((on (=~~) T.toStrict) content) patterns


Now it's time to add import Text.Regex.TDFA.Text.Lazy and remove text conversions

- matches patterns content = map T.fromStrict
-   $ concatMap ((on (=~~) T.toStrict) content) patterns
+ matches patterns content = concatMap (content=~~) patterns


You can swap arguments and drop patterns varaible.

As for the analyse function, it's just a matter of taste but I think it is more readable to enumerate all variants in single case:

analyze likes dislikes text
  = case (matches text likes, matches text dislikes) of
    ([], _)       -&gt; Left []
    (liked, [])   -&gt; Right liked
    (_, disliked) -&gt; Left disliked


For the last step you can import Text type unqaulified to make function types look cleaner.

Here is the result:

import Data.Text.Lazy (Text)
import qualified Data.Text.Lazy as T
import Text.Regex.TDFA.Text.Lazy
import Text.Regex.TDFA

matches :: Text -&gt; [Text] -&gt; [Text]
matches content = concatMap ((=~~) content)

analyze :: [Text] -&gt; [Text] -&gt; Text -&gt; Either [Text] [Text]
analyze likes dislikes text
  = case map (matches text) [likes, dislikes] of
    [[], _]       -&gt; Left []
    [liked, []]   -&gt; Right liked
    [_, disliked] -&gt; Left disliked