所有regex在Haskell的比赛

时间:2021-04-27 19:35:46

According to a number of tutorials (including Real World Haskell) one can, using ghci do the following

根据许多教程(包括真实世界的Haskell),可以使用ghci完成以下操作

ghci > :m Text.Regex.Posix
ghci > "foo foo foo" =~ "foo" :: [String]
["foo","foo","foo"]

Yet, when I attempt this, it yields

然而,当我尝试这样做时,它会让步

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]

What is the correct way of obtaining a list of all matches in haskell?

在haskell中获得所有匹配列表的正确方法是什么?

1 个解决方案

#1


24  

The regex libraries can be somewhat confusing with their overloaded return types, but to get all the matches you just need to ensure that the return type is AllTextMatches, for example:

regex库可能会对其重载的返回类型感到困惑,但要获得所有匹配,只需确保返回类型是AllTextMatches,例如:

Prelude> :m + Text.Regex.Posix
Prelude Text.Regex.Posix> getAllTextMatches $ "foo foo foo" =~ "foo" :: [String]
["foo","foo","foo"]

#1


24  

The regex libraries can be somewhat confusing with their overloaded return types, but to get all the matches you just need to ensure that the return type is AllTextMatches, for example:

regex库可能会对其重载的返回类型感到困惑,但要获得所有匹配,只需确保返回类型是AllTextMatches,例如:

Prelude> :m + Text.Regex.Posix
Prelude Text.Regex.Posix> getAllTextMatches $ "foo foo foo" =~ "foo" :: [String]
["foo","foo","foo"]