According to a number of tutorials (including Real World Haskell) one can, using ghci do the following
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?
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:
Prelude> :m + Text.Regex.Posix
Prelude Text.Regex.Posix> getAllTextMatches $ "foo foo foo" =~ "foo" :: [String]
["foo","foo","foo"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With