Effacer les filtres
Effacer les filtres

Parsing string into cell array "bash-script style"

2 vues (au cours des 30 derniers jours)
Chad Gilbert
Chad Gilbert le 23 Mai 2013
I'd like to parse a custom scripting language, which reliably formats its various commands in the following format:
cmd arg1 arg2 ... argN
wheree different commands may have different numbers of arguments. For most arguments, this is pretty easy to do using strsplit(), as they are either numbers or single words separated by spaces. However, users can use multiple words as a single argument by surrounding it with double quotes (e.g. "single arg"). Is there a nice way for me to parse this? That is, can I take:
cmd 0.1 hello "single arg"
and return
ans =
'cmd' [0.1] 'hello' 'single arg'
as opposed to what I get now:
ans =
'cmd' [0.1] 'hello' '"single' 'arg"'
Any help or ideas are appreciated, thanks!
Chad

Réponse acceptée

Cedric
Cedric le 24 Mai 2013
Modifié(e) : Cedric le 24 Mai 2013
Your solution is not working on my system; "there you" is split in "there and you" (essentially because the \S+ expression is verified with both "there and you", which shortcuts the 1st expression in the OR statement).
I think that what you want is:
>> c = regexp('Hello 0.1 "there you" are', '(?<=").*?(?=")|[^"\s]+', 'match')
c =
'Hello' '0.1' 'there you' 'are'
  1 commentaire
Chad Gilbert
Chad Gilbert le 27 Mai 2013
Oops! Agreed. I had a typo. Thanks :)

Connectez-vous pour commenter.

Plus de réponses (0)

Tags

Produits

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by