[racket] Use regexps or something else?

From: Eli Barzilay (eli at barzilay.org)
Date: Sat Jun 4 11:50:14 EDT 2011

11 hours ago, Rodolfo Carvalho wrote:
> Imagine an scenario that instead of 2 (as above) we had 20 columns
> of a table, and we wanted to return a table of the squares of the
> values for all columns, except for columns 12 and 15 (of course we
> could parse them out and ignore then when processing/returning, but
> you get the idea...)

A classical mistake that people do when they start playing with
regexps is they forget about the rest of the language and try to do
too much with regexps.  If you have a table with more than 2 numbers,
I'd do it like this:

  (define t "
    23 12 72367 2772 7272 7777 2.342
    15 45 1e2 1e3 1e4
    32 27 3.1415926")

  (map (λ(l) (map string->number (regexp-match* #px"\\d+" l)))
       (regexp-split #rx"\n" t))

And now tweaking and extending it is much easier, for example, there's
no need to count "\\d+\\s+" and change it when the input changes, you
just do it at the post-regexp level.  Similar to making up a parser by
a level of simple lexing, and another level for the grammar.

          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                    http://barzilay.org/                   Maze is Life!

Posted on the users mailing list.