[Haskell-beginners] Text.Regex.TDFA docs?

Mike Meyer mwm at mired.org
Fri Jun 3 07:41:25 CEST 2011


I'm running ghc 7.0.3 on FreeBSD, and trying to figure out Text.Regex
- pretty much any flavor, actually.

Based on this http://www.haskell.org/haskellwiki/Regular_expressions,
it appears that TDFA is probably the right library to use, though
Posix seems to be popular with the tutorials.

There's a couple of documents around:

http://www.serpentine.com/blog/2007/02/27/a-haskell-regular-expression-tutorial/
http://book.realworldhaskell.org/read/efficient-file-processing-regular-expressions-and-file-name-matching.html

But it appears to be dated, as parts of them just don't work, using
either TDFA or Posix. For instance, both imply that [String] ought to
give me a list of matches, but that doesn't work in either Posix or TDFA.

Prelude Text.Regex.TDFA> "this foo is the foo" =~ "foo" :: [String]

<interactive>:1:23:
    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: "this foo is the foo" =~ "foo" :: [String]
    In an equation for `it':
        it = "this foo is the foo" =~ "foo" :: [String]
Prelude Text.Regex.TDFA> :m -Text.Regex.TDFA
Prelude> :m +Text.Regex.Posix
Prelude Text.Regex.Posix> "this foo is the foo" =~ "foo" :: [String]

<interactive>:1:23:
    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: "this foo is the foo" =~ "foo" :: [String]
    In an equation for `it':
        it = "this foo is the foo" =~ "foo" :: [String]
Prelude Text.Regex.Posix> 

Come to think of it, this seems to be the case for most of the
[<type>] types - except that [[String]] seems to work. I.e. - (Int,
Int) works, but [(Int, Int)] doesn't; (MatchOffset, MatchLength)
works, but [(MatchOffset, MatchLength)] doesn't.

The docs here
http://hackage.haskell.org/packages/archive/regex-base/0.93.2/doc/html/Text-Regex-Base-Context.html

Seem to imply that there are types not mentioned in the tutorials, but
trying to use most of those (actually, it looks like anything using
All*) generates errors because the resulting type isn't an instance of
Show:

Prelude Text.Regex.Posix> "this foo is the foo" =~ "foo" :: AllTextSubmatches [] String

<interactive>:1:1:
    No instance for (Show (AllTextSubmatches [] String))
      arising from a use of `print'
    Possible fix:
      add an instance declaration for
      (Show (AllTextSubmatches [] String))
    In a stmt of an interactive GHCi command: print it

So - how do I go about getting a list of all matches (the tutorials
[String] behavior) or a list of offset/length tuples (the tutorials
[(MatchOffset, MatchLength)] behavior?

       thanks,
       <mike
-- 
Mike Meyer <mwm at mired.org>		http://www.mired.org/
Independent Software developer/SCM consultant, email for more information.

O< ascii ribbon campaign - stop html mail - www.asciiribbon.org



More information about the Beginners mailing list