Haskell:aeson,OverloadedStrings和Text.Regex.PCRE

Jam*_*rbo 1 regex haskell aeson

我有一个正则表达式使用Text.Regex.PCRE工作正常:

[[_,_id,_name]] = "199mercury" =~ "(\\d+)(\\w+) :: [[String]]
Run Code Online (Sandbox Code Playgroud)

但是,我在{ - #LANGUAGE OverloadedStrings# - }中添加了使用aeson(json库)并在=〜上获取实例错误:

<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+)"
Run Code Online (Sandbox Code Playgroud)

搜索修复程序似乎是将正则表达式更改为:

getAllTextSubmatches ("199mercury" =~ "(\\d+(\\w+)" :: AllTextSubmatches [] String)
Run Code Online (Sandbox Code Playgroud)

但这似乎只是给我另一个实例错误:

   No instances for (RegexMaker Regex CompOption ExecOption source0,
                      RegexContext Regex source10 (AllTextSubmatches [] String))
Run Code Online (Sandbox Code Playgroud)

什么是正确的类型放在这里?我尝试的任何东西似乎都没有.似乎OverloadedStrings是问题,但我找不到任何解决方案,只是使用Data.Text.pack与aeson,这有效,但我想弄清楚我正在做什么错误的正则表达式.我很好奇是否是Text.Regex无法与OverloadedStrings一起使用的问题,但是我找不到任何证据.

Eri*_*ikR 9

它不漂亮,但这种类型检查:

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

quux = ("1999mercury" :: String) =~ ("(\\d+)(\\w+)" :: String) :: [[String]]
Run Code Online (Sandbox Code Playgroud)

您还可以创建单形版本=~以避免始终编写类型:

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

quux = "1999mercury" `matches` "(\\d+)(\\w+)"
Run Code Online (Sandbox Code Playgroud)