Question

I have a regex using Text.Regex.PCRE which worked fine:

[[_,_id,_name]] = "199mercury" =~ "(\\d+)(\\w+) :: [[String]]

However, I added in {-# LANGUAGE OverloadedStrings #-} to use aeson (json library) and get an instance error on =~:

<interactive>:33:14:
    No instances for (RegexMaker Regex CompOption ExecOption source0,
                      RegexContext Regex source10 target0)
      arising from a use of `=~'
    Possible fix:
      add instance declarations for
      (RegexMaker Regex CompOption ExecOption source0,
       RegexContext Regex source10 target0)
    In the expression: "199mercury" =~ "(\\d+(\\w+)"
    In an equation for `it': it = "199mercury" =~ "(\\d+(\\w+)"

Searching around the fix seems to be to change the regex to:

getAllTextSubmatches ("199mercury" =~ "(\\d+(\\w+)" :: AllTextSubmatches [] String)

But that seems to just give me another instance error:

   No instances for (RegexMaker Regex CompOption ExecOption source0,
                      RegexContext Regex source10 (AllTextSubmatches [] String))

What's the right types to put in here? Nothing I try seems to do the trick. It seems OverloadedStrings is the problem but I can't find any solutions other than to just use Data.Text.pack with aeson, which works but I want to figure out what I am doing wrong with the regex. I'm curious if it is some issue where Text.Regex doesn't work with OverloadedStrings, but I can't find any evidence for that.

Was it helpful?

Solution

It's not pretty, but this type checks:

{-# LANGUAGE OverloadedStrings #-}    
import Text.Regex.PCRE

quux = ("1999mercury" :: String) =~ ("(\\d+)(\\w+)" :: String) :: [[String]]

You can also create a monomorphic version of =~ to avoid writing the types all the time:

matches :: String -> String -> [[String]]
matches = (=~)

quux = "1999mercury" `matches` "(\\d+)(\\w+)"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top