G'day proxie,
Welcome to the Monastery.
"Can someone help me get started on the most efficient reg exps to do this?"
When Perl's string handling functions can do the job,
you'll generally find that they'll be measurably, more efficient than regular expressions.
Here's a solution that doesn't use regular expressions.
Use Benchmark to compare the efficiency of this code
against other posted solutions.
#!/usr/bin/env perl
use strict;
use warnings;
my $cmd = q{execute test_number_1 -tex -tex_args -sub_args +debug_dir=
+./ -sub_args +debug_dir=./ -constraint parity_en,random_en -sub_args
+'"' ruck=1 '"' -constraint dual_en -sub_args -cd -sub_args 2596.slow
+-sub_args test -seed 1 -tex_args- -opt 1 -tag 2};
my $TAB = ' ' x 4;
my $depth = 0;
my @tokens = split ' ', $cmd;
my @parts;
cmd_format(\@tokens, \@parts, $depth);
print "$_\n" for @parts;
sub cmd_format {
my ($tokens, $parts, $depth) = @_;
for (my $i = 0; $i <= $#$tokens; ++$i) {
my $token = $tokens->[$i];
if (-1 == index $token, '-', 0) {
my @starts = ($token);
while (-1 == index $tokens[$i + 1], '-', 0) {
push @starts, $tokens[++$i];
}
push @$parts, $TAB x $depth . "@starts";
}
elsif ($token eq '-tex') {
push @$parts, $TAB x ($depth + 1) . $token;
cmd_format([@{$tokens}[$i + 1 .. $#$tokens]], $parts, $dep
+th + 2);
last;
}
elsif ($token eq '-tex_args') {
push @$parts, $TAB x $depth . $token;
my $args = [];
while ($tokens->[$i + 1] ne '-tex_args-') {
push @$args, $tokens->[++$i];
}
cmd_format($args, $parts, $depth + 1);
push @$parts, $TAB x $depth . $tokens->[++$i];
}
else {
my @opts = ($token, $tokens->[++$i]);
for ($i + 1 .. $#$tokens) {
last unless -1 == index $tokens->[$i + 1], '-', 0;
push @opts, $tokens->[++$i];
}
push @$parts, $TAB x $depth . "@opts";
}
}
}
Output:
execute test_number_1
-tex
-tex_args
-sub_args +debug_dir=./
-sub_args +debug_dir=./
-constraint parity_en,random_en
-sub_args '"' ruck=1 '"'
-constraint dual_en
-sub_args -cd
-sub_args 2596.slow
-sub_args test
-seed 1
-tex_args-
-opt 1
-tag 2