I want to parse commands from a text file. The commands are of the form: command "param 1" "param 2" command "param" command "param 1" "param 2" "param 3" Each command will have one or more parameters. Each parameter is enclosed in "" marks, and may be one or more words. Is there a regular expression that will match each command without knowing ahead of time how many parameters there are? I tried something like this: line =~ /^(\S+)(?:\s+\"([^\"]*)")+$/ But that only matches the command and the last parameter; I believe $2 is being overwritten as the match progresses. Is there a way to do this with a single regular expression? Thanks, Adam
on 2009-05-06 09:24
on 2009-05-06 11:30
On Wed, May 6, 2009 at 7:23 AM, Adam B. <firstname.lastname@example.org> wrote: > But that only matches the command and the last parameter; I believe $2 is > being overwritten as the match progresses. Is there a way to do this with a > single regular expression? I could not come up with one, but why would you need a single regular expression? command, params = line.split( /\s+/, 2 ) [ command ] + params.scan(/\s*"(.*?)"/).flatten seems to work fine. HTH Robert -- Si tu veux construire un bateau ... Ne rassemble pas des hommes pour aller chercher du bois, préparer des outils, répartir les tâches, alléger le travail… mais enseigne aux gens la nostalgie de l’infini de la mer. If you want to build a ship, don’t herd people together to collect wood and don’t assign them tasks and work, but rather teach them to long for the endless immensity of the sea.