操作系统:MacOSX 10.7.1来自brew的GHC和Haskell平台.
GHCi, version 7.0.4: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.
Prelude> :m +Text.Regex.Posix
Prelude Text.Regex.Posix> "foo" =~ "o" :: [String]
<interactive>:1:7:
No instance for (RegexContext Regex [Char] [String])
arising from a use of `=~'
Possible fix:
add an instance declaration for
(RegexContext Regex [Char] [String])
In the expression: "foo" =~ "o" :: [String]
In an equation for `it': it = "foo" =~ "o" :: [String]
Prelude Text.Regex.Posix> "foo" =~ "o" :: String
Loading package array-0.3.0.2 ... linking ... done.
Loading package bytestring-0.9.1.10 ... linking ... done.
Loading package containers-0.4.0.0 ... linking ... done.
Loading package transformers-0.2.2.0 ... linking ... done.
Loading package mtl-2.0.1.0 ... linking ... done.
Loading package regex-base-0.93.2 ... linking ... done.
Loading package regex-posix-0.95.1 ... linking ... done.
"o"
Run Code Online (Sandbox Code Playgroud)
我相信图书馆更新了.我认为输出"foo" =~ "o" :: [String]是["o", "o"]
任何建议将不胜感激.
ram*_*ion 11
ghci> getAllTextMatches ("foo" =~ "o" :: AllTextMatches [] String)
["o", "o"]
Run Code Online (Sandbox Code Playgroud)
我没有在Haskell中使用正则表达式(我认为Dan Burton的答案更为惯用).
所以我想出来的方式是我看了你的类型错误No instance for (RegexContext Regex [Char] [String]),并弹出到ghci:
ghci> :t (=~)
(=~)
:: (RegexMaker Regex CompOption ExecOption source,
RegexContext Regex source1 target) =>
source1 -> source -> target
Run Code Online (Sandbox Code Playgroud)
所以RegexContext Regex [Char] [String]是一个包含返回类型的类"foo" =~ "o" :: [String].所以我看看这个类确实存在哪些实例,所以我可以找出允许返回值的内容:
ghci> :i RegexContext
class RegexLike
regex source => RegexContext regex source target where
match :: regex -> source -> target
matchM :: Monad m => regex -> source -> m target
-- Defined in Text.Regex.Base.RegexLike
instance RegexContext Regex String String
-- Defined in Text.Regex.Posix.String
instance RegexLike a b => RegexContext a b [[b]]
-- Defined in Text.Regex.Base.Context
...
instance RegexLike a b => RegexContext a b (AllTextMatches [] b)
-- Defined in Text.Regex.Base.Context
...
Run Code Online (Sandbox Code Playgroud)
这个AllTextMatches名字似乎表明你在寻找什么,所以我检查了一下:
ghci> :i AllTextMatches
newtype AllTextMatches f b
= AllTextMatches {getAllTextMatches :: f b}
-- Defined in Text.Regex.Base.RegexLike
instance RegexLike a b => RegexContext a b (AllTextMatches [] b)
-- Defined in Text.Regex.Base.Context
Run Code Online (Sandbox Code Playgroud)
所以这是用于提取所有文本匹配的类型,正如我怀疑的那样.我需要做的就是表明我想要一个这种类型的返回值.
还要注意可能的返回类型[[b]],我假设它返回包含每个完整匹配及其所有子匹配的列表列表:
ghci> "foo" =~ "o" :: [[String]]
[["o"],["o"]]
ghci> "foo bar baz" =~ "[aeiou](.)" :: [[String]]
[["oo","o"],["ar","r"],["az","z"]]
Run Code Online (Sandbox Code Playgroud)
所以也许这是你打算使用的类型,而不是[String].我可以看到存在[String]时有点模棱两可[[String]]- 应该"foo bar baz" =~ "[aeiou](.)" :: [String]是fst ("foo bar baz" =~ "[aeiou](.)" :: [[String]])或者map fst ("foo bar baz" =~ "[aeiou](.)" :: [[String]]).
这也有效
ghci> getAllTextMatches $ "foo" =~ "o" :: [String]
["o","o"]
Run Code Online (Sandbox Code Playgroud)
尝试这个:
Prelude Text.Regex.Posix> getAllTextMatches ("foo" =~ "o" :: AllTextMatches [] String)
["o","o"]
Run Code Online (Sandbox Code Playgroud)