【问题标题】:All matches of regex in HaskellHaskell中正则表达式的所有匹配项
【发布时间】:2014-01-23 05:15:55
【问题描述】:

根据一些教程(包括 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中获取所有匹配列表的正确方法是什么?

【问题讨论】:

标签: regex haskell


【解决方案1】:

正则表达式库可能与其重载的返回类型有些混淆,但要获得所有匹配项,您只需确保返回类型为 AllTextMatches,例如:

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

【讨论】:

  • 嗯,这行得通。谢谢你。您知道为什么教程中似乎没有提到这一点吗?
  • @ehird 我在哪里可以找到最新的参考资料?
  • Here 是解决此问题的宝贵示例。
猜你喜欢
  • 2016-11-04
  • 2021-10-12
  • 2013-05-09
  • 1970-01-01
  • 2020-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多