[racket] Split string except quoted
shlex is for shell-like lexers, but you can use Racket's lexer to achieve
what you want in this particular case:
(port->list read (open-input-string "one two three \"four five\""))
; -> '(one two three "four five")
You get symbols and strings, but you can convert them to strings only if
you want:
(map ~a (port->list read (open-input-string "one two three \"four five\"")))
; -> '("one" "two" "three" "four five")
This won't work for single-quoted string though, because single quotes
don't have a string meaning in Lisp/Scheme/Racket:
(port->list read (open-input-string "one two three 'four five'"))
; -> Error: read: expected an element for quoting ' (found end-of-file)
You can then use the parser tools:
http://docs.racket-lang.org/parser-tools/
HTH
Laurent
On Fri, Aug 16, 2013 at 10: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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20130818/03f6b1db/attachment.html>