多个正则表达式与Haskell中的[String]不匹配

时间:2018-12-29 19:31:34

标签: regex haskell

我正在看RWH教程here,该教程建议使用static List getMetals() { grails.util.Holders.config.metals } 返回多个结果,但有一个错误。如您所见:

[String]

生产

"I'd like to group by word breaks" =~ "\\S+" :: Bool
"I'd like to group by word breaks" =~ "\\S+" :: String
"I'd like to group by word breaks" =~ "\\S+" :: [[String]]

分别。

但是推荐的True "I'd" [["I'd"],["like"],["to"],["group"],["by"],["word"],["breaks"]] 却没有,而是出现错误:

[String]

我该如何请求缺少的"I'd like to group by word breaks" =~ "\\S+" :: [String] <interactive>:1:1: error: • No instance for (RegexContext Regex String [String]) arising from a use of ‘=~’ • In the expression: "I'd like to group by word breaks" =~ "\\S+" :: [String] In an equation for ‘it’: it = "I'd like to group by word breaks" =~ "\\S+" :: [String] 型建议,该建议将提供我想要的东西,即:

[String]

无需后处理?同样,有趣的是,在其他成功的类型转换的背景下,这似乎很自然,甚至在本书写作时的某个时候也是如此,并且不再适用。更改的解释是什么?

1 个答案:

答案 0 :(得分:1)

从评论中看来,建议是:

Prelude Text.Regex.PCRE> getAllTextMatches ("I'd like to group by word breaks" =~ "\\S+") :: [String]
["I'd","like","to","group","by","word","breaks"]

Prelude Text.Regex.PCRE> concat $ "I'd like to group by word breaks" =~ "\\S+" :: [String]
["I'd","like","to","group","by","word","breaks"]

这些都不像以前那样干净。