Question

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 ?

Était-ce utile?

La 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

Autres conseils

So the trick is to modify integers to simply skip letters.

integer :: Parser Int
integer =
  many letter *>
  ((read . concat) <$> many digit `sepBy` many1 letter)
  <* many letter

This handles 12a34 correctly. Otherwise something as easy as

 many letter *> (read <$> many digit) <* many letter
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top