Regex & String Libraries in Haskell

后端 未结 4 836
你的背包
你的背包 2021-02-10 10:32

I\'m trying to introduce Haskell into my daily life by using it to write incidental scripts and such.

readProcess is handy for getting the results of exterior commands,

4条回答
  •  Happy的楠姐
    2021-02-10 11:13

    I found this to be a good starting point: http://www.serpentine.com/blog/2007/02/27/a-haskell-regular-expression-tutorial/ It only covers the basics, no advanced topics, but it's great to get started IMHO.

    Things to note:

    • Regexes in haskell are different in that they have overloaded return types. This means that you can pull many different kinds of thing out of a regex match. (Bool, String, [String], etc...) Depending on the return type you use, it will give you back a different kind of answer (whether or not the regex matched, the test of the match, all matching subgroups, etc..) This is done using some fairly complex typeclass voodoo. The above link demonstrates the basic kinds, a more complete list is here
    • There are actually multiple standard modules in haskell that provide regex support (strange but true). The tutorial above shows the POSIX module, because it comes standard in haskell. If you have cabal, you can also pretty easily install other regex modules and use those instead. There's a pcre binding (regex-pcre), as well as some packages that work via DFAs (regex-dfa, among others). Install using a command like: cabal install regex-pcre and you should be good to go.
      • (The modules have a standardized interface, the difference is mainly in the implementation and the regex flavor)
    • There IS a regex object in haskell, but you don't really need it to use the =~ or =~~ match operators. (Just use a string, conversion happens automatically). If your task is complicated enough that you want a first class parsing object, consider looking into Parsec as has been mentioned in other answers.

    DISCLAIMER: I only really user pcre, myself, so I don't really know much about the other packages.

提交回复
热议问题