1

If I'm given a string of data

"data-symbol='1'
 data-symbol='2'
 data-symbol='3'
 data-symbol='4' "

How can I take the numbers out of the string using regexp-match and put them into a list '(1 2 3 4), order isn't necessary. Also as for the numbers they could be anything they're just used as an example.

1 Answers1

4

You could use regexp-match* along with capturing groups and string->number to extract the information from your string:

> (map string->number
       (regexp-match* #px"data-symbol='(\\d+)'"
                      "data-symbol='1'
                       data-symbol='2'
                       data-symbol='3'
                       data-symbol='4'"
                      #:match-select second))
'(1 2 3 4)

The regexp-match* function finds all matches of a regular expression in a given string, and #:match-select is used to pick which capture group(s) to return in the result. Then string->number is used to convert each element of the match to a numeric value.

Be careful, though: it looks like you might be trying to parse HTML with regular expressions, and as was (in)famously noted on this very site, that way leads only to madness. Consider using one of Racket’s HTML parsing libraries instead.

Community
  • 1
  • 1
Alexis King
  • 43,109
  • 15
  • 131
  • 205