根据一些教程(包括 Real World Haskell)一个可以,使用 ghci 执行以下操作
ghci > :m Text.Regex.Posix
ghci > "foo foo foo" =~ "foo" :: [String]
["foo","foo","foo"]
然而,当我尝试这样做时,它会产生
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: "abc" =~ "ab" :: [String]
In an equation for `it': it = "abc" =~ "ab" :: [String]
在haskell中获取所有匹配项列表的正确方法是什么?
请您参考如下方法:
正则表达式库可能与其重载的返回类型有些混淆,但要获得所有匹配项,您只需确保返回类型为 AllTextMatches
, 例如:
Prelude> :m + Text.Regex.Posix
Prelude Text.Regex.Posix> getAllTextMatches $ "foo foo foo" =~ "foo" :: [String]
["foo","foo","foo"]