Skip to content

Parsing a list with no brackets #461

Answered by c-cube
dalps asked this question in Q&A
Dec 5, 2024 · 2 comments · 2 replies
Discussion options

You must be logged in to vote

I have this alternative if you like:

# module P = CCParse
# let s = "1 2\n3 4\n5   6\n7  8";;
val s : string = "1 2\n3 4\n5   6\n7  8"
# P.parse_string P.(each_line (both (U.int <* many space) U.int)) s;;
- : ((int * int) list, string) result = Ok [(1, 2); (3, 4); (5, 6); (7, 8)]

If there are empty lines it's a bit more annoying (each_line is somewhat rigid) but you can get options and filter later with CCList.keep_some:

# let s = "1 2\n3 4\n5   6\n7  8\n";;
val s : string = "1 2\n3 4\n5   6\n7  8\n"
# P.parse_string P.(each_line (try_opt @@ both (U.int <* many space) U.int)) s;;
- : ((int * int) option list, string) result =
Ok [Some (1, 2); Some (3, 4); Some (5, 6); Some (7, 8); None]

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@dalps
Comment options

@dalps
Comment options

Answer selected by dalps
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants