Search code examples
regexhaskelltextpcre

How to work with Data.Text via pcre-heavy


I need a simple example to work with Data.Text via prce-heavy. My example is not working, why?

{-# LANGUAGE OverloadedStrings, QuasiQuotes, FlexibleContexts #-}

import Data.Text
import Text.Regex.PCRE.Heavy
import qualified Data.Text.IO as TIO

main = do
    let text = "xxx@yahoo.com" :: Text
    let 
      text2 :: Text
      text2 = gsub ([re|xxx|]) ("yyy" :: Text) text
    TIO.putStrLn text2

But the same code with String work well:

{-# LANGUAGE QuasiQuotes, FlexibleContexts #-}

import Text.Regex.PCRE.Heavy

main = do
    let text = "xxx@yahoo.com" :: String
    let 
      text2 :: String
      text2 = gsub ([re|xxx|]) ("yyy" :: String) text
    putStrLn text2

Errror for 1st sampe:

textproblem.hs:11:15: error:
    * No instance for (Data.String.Conversions.ConvertibleStrings
                         Data.String.Conversions.SBS Text)
        arising from a use of `gsub'
    * In the expression:
        gsub
          ((Text.Regex.PCRE.Light.compile
              (Data.String.Conversions.cs ("xxx" :: String))
              [read "PCREOption {unPCREOption = 2048}" :: PCREOption]))
          ("yyy" :: Text)
          text
      In an equation for `text2':
          text2
            = gsub
                ((Text.Regex.PCRE.Light.compile
                    (Data.String.Conversions.cs ("xxx" :: String))
                    [read "PCREOption {unPCREOption = 2048}" :: PCREOption]))
                ("yyy" :: Text)
                text
      In the expression:
        do { let text = ...;
             let text2 :: Text
                 text2
                   = gsub
                       ((Text.Regex.PCRE.Light.compile
                           (Data.String.Conversions.cs (...)) ...))
                       ("yyy" :: Text)
                       text;
             TIO.putStrLn text2 }

Solution

  • After reinstalling Haskell Platform to recent version and adding pcre library to msys2 and some experiments, I can say, that this sample works well (Haskell Platform on Windows-7 (64-bit) with ghc 8.4.3, msys2 with pcre.h and libpcre.a added via pacman, and installed carefully pcre-light and pcre-heavy). On Debian Linux (64-bit) it works well, too, and without any tricks. Why I saw a previous error -- I have no idea, maybe, except problem with older version.