2013-04-04 53 views
1

我有使用Text.Regex.PCRE其精细的工作正则表达式:哈斯克尔:埃宋,OverloadedStrings和Text.Regex.PCRE

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

然而,我在加入{ - #语言OverloadedStrings# - }使用埃宋(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+)" 

周围的固定搜索似乎是正则表达式更改为:

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

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

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

什么是正确的类型放在这里?我所尝试的任何东西似乎都无能为力。看起来OverloadedStrings是问题,但我找不到任何解决方案,只是使用Data.Text.pack与aeson,它的工作原理,但我想弄清楚我正在做的错误与正则表达式。我很好奇,如果实在是有些问题,即Text.Regex不OverloadedStrings工作,但我无法找到任何证据。

回答

9

这不是很漂亮,但这种类型的检查:

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

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

您还可以创建的=~单态版本,以避免编写类型所有的时间:

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

quux = "1999mercury" `matches` "(\\d+)(\\w+)"