Search code examples
parsinghaskellparsec

Ignoring letters and parsing only numbers using Parsec


This code works only when numerals (eg: "1243\t343\n") are present:

tabFile = endBy line eol
line = sepBy cell (many1 tab)
cell = integer
eol = char '\n'

integer = rd <$> many digit
  where rd = read :: String -> Int

Is there a way to make it parse "abcd\tefg\n1243\t343\n" such that it ignores the "abcd\tefg\n" part ?


Solution

  • You can skip everything except digits using skipMany. Something like the next:

    many (skipMany (noneOf ['0'..'9']) >> digit)
    

    or (depending on what you actually need)

    skipMany (noneOf ['0'..'9']) >> many digit