2013-04-04 52 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+)"