haskell - Parsec how to find "matches" within a string -
how can use parsec parse matched input in string , discard rest?
example: have simple number parser, , can find numbers if know separates them:
num :: parser int num = read <$> many digit parse (num `sepby` space) "" "111 4 22"
but if don't know between numbers?
"i live 111 years <b>old</b> if work out 4 days week starting @ 22."
many anychar
doesn't work separator, because consumes everything.
so how can things match arbitrary parser surrounded things want ignore?
edit: note in real problem, parser more complicated:
optiontag :: parser fragment optiontag = string "<option" manytill anychar (string "value=") n <- many1 digit manytill anychar (char '>') chapterprefix text <- many1 (noneof "<>") return $ option (read n) text chapterprefix = many digit >> char '.' >> many space
for arbitrary parser myparser
, it's quite easy:
solution = many (let 1 = myparser <|> (anychar >> one) in one)
it might clearer write way:
solution = many loop loop = myparser <|> (anychar >> loop)
essentially, defines recursive parser (called loop
) continue searching first thing can parsed myparser
. many
search exhaustively until failure, ie: eof.