[racket] Split string except quoted

From: Stephen Chang (stchang at ccs.neu.edu)
Date: Fri Aug 16 17:12:26 EDT 2013

This will give you what you want, though I'm sure there's a better
way. You probably want a lexer.

(define matches
  (regexp-match* #px"([^\\s\"]+)|\"([^\"]*)\""
                 "one two three \"four five\""
                 #:match-select cdr))
(map (λ (lst) (first (filter (λ (x) x) lst))) matches)

On Fri, Aug 16, 2013 at 4:16 PM, Richard Parsons
<richard.lee.parsons at gmail.com> wrote:
> Hi there, I'm new to Racket.
>
> I'm trying to split strings on whitespace except when quoted, such as this:
>
> "one two three \"four five\"" => '("one" "two" "three" "four five")
>
> In Python, this could be done with shlex.split().
>
> Perhaps I could do it with a lexer?
>
> Perhaps I could do it with regular expressions?
>
> Perhaps the source code for current-command-line-arguments could show me how
> to do it, although I can't work out how to locate it.
>
> Any suggestions anyone?
>
> Many thanks
> Richard
>
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users
>


Posted on the users mailing list.