So, let’s say I have the strings:
aaa -Dprop=var Class arg
aaa Class arg
I want a single one-liner (perl, sed, awk, doesn’t matter) that can extract arg and var (if its there). Specifically, I’d like to return
arg var
arg
The following works for the first one:
echo "aaa -Dprop=var Class arg" | perl -pe 's|.*(-Dprop=([a-z]*)).*Class (.*)|\3 \2|'
but because -Dprop= is required, it obviously doesn’t work the second one.
If, however, I make that match optional:
echo "aaa -Dprop=var Class arg" | perl -pe 's|.*(-Dprop=([a-z]*))?.*?Class (.*)|\3 \2|'
it doesn’t work for the first one because, I believe the two .*s are greedy qualifiers and match -Dprop first.
If I make them non-greedy, it still doesn’t work, but I’m not sure why.
echo "aaa -Dprop=var Class arg" | perl -pe 's|.*?(-Dprop=([a-z]*))?.*?Class (.*)|\3 \2|'
So, first, what regex can I use that matches correctly? (I know I could split it into multiple commands, but I rather just have one).
Perhaps:
Notice too that \3 is better written as $3 (which the warnings pragma would divulge).