I have a string that looks like this
"21 4\n21 2 _ _ 19 11\n 12 _ _ 1 _ _\n_ _ _ 7 13 _"
(there is a blank space between 21 and 4, 21 and 2, 2 and _, etc)
I would like to loop through it, extract each number, character _ or \n.
(basically split the string on the blank spaces and on the \n)
Using substring will not work because some elements have more than one character (like 21 or 13)
If the easiest way would be to convert the string into a list like
["21" "4" "\n" "21" "2" "_" "_" "19" "11" "\n"...]
that would be fine but not sure how to do it.
Here is something to get you started. This does not use regular expressions - just repeated use of read
.
#lang racket
(define (parse-line line)
(define in (open-input-string line))
(for/list ([sym (in-port read in)])
(~a sym)))
(define (parse str)
(define in (open-input-string str))
(for/list ([line (in-lines in)])
(parse-line line)))
(parse "21 4\n21 2 _ _ 19 11\n 12 _ _ 1 _ _\n_ _ _ 7 13 _")
The output is:
'(("21" "4")
("21" "2" "_" "_" "19" "11")
("12" "_" "_" "1" "_" "_")
("_" "_" "_" "7" "13" "_"))