此代码仅在"1243\t343\n"
存在数字(例如:)时才有效:
tabFile = endBy line eol
line = sepBy cell (many1 tab)
cell = integer
eol = char '\n'
integer = rd <$> many digit
where rd = read :: String -> Int
有没有办法让它解析"abcd\tefg\n1243\t343\n"
以忽略该"abcd\tefg\n"
部分?
您可以使用skipMany跳过除数字之外的所有内容。类似下一个:
many (skipMany (noneOf ['0'..'9']) >> digit)
或(取决于您实际需要什么)
skipMany (noneOf ['0'..'9']) >> many digit
所以诀窍是修改整数以简单地跳过字母。
integer :: Parser Int
integer =
many letter *>
((read . concat) <$> many digit `sepBy` many1 letter)
<* many letter
这可以12a34
正确处理。否则像
many letter *> (read <$> many digit) <* many letter