In this section, we assume that you are already familiar with regular expressions by way of some other language, such as Python, Perl, or Java.
For brevity, we will abbreviate “regular expression” as regexp from here on.
Rather than introduce regexps as something new, we will focus on what’s different about regexp handling in Haskell, compared to other languages. Haskell’s regular expression matching libraries are a lot more expressive than those of other languages, so there’s plenty to talk about.
To begin our exploration of the regexp
libraries, the only module we’ll need to work with is
As usual, the most convenient way to explore this module is by
interacting with it via ghci:
The only function that we’re likely to
need for normal use is the regexp matching function, an infix operator
(=~) (borrowed from Perl).
The first hurdle to overcome is that Haskell’s regexp libraries make
heavy use of polymorphism. As a result, the type signature of the
(=~) operator is difficult to understand, so we will not explain it
=~ operator uses typeclasses for both of its
arguments and also for its return type. The first argument (on the left
=~) is the text to match;
the second (on the right) is the regular expression to match against. We
can pass either a String or a ByteString as
=~ operator is polymorphic in its return type, so the Haskell compiler needs ...